Skip to content

Commit 0cace89

Browse files
committed
string and enum
1 parent e5831cc commit 0cace89

10 files changed

Lines changed: 42 additions & 31 deletions

File tree

exercises/enums/enums1.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22
//
33
// No hints this time! ;)
44

5-
// I AM NOT DONE
65

76
#[derive(Debug)]
87
enum Message {
98
// TODO: define a few types of messages as used below
9+
Quit,
10+
Move { x: i32,y: i32},
11+
Write(String),
12+
ChangeColor(i32,i32,i32),
1013
}
1114

1215
fn main() {

exercises/enums/enums2.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
// Execute `rustlings hint enums2` or use the `hint` watch subcommand for a
44
// hint.
55

6-
// I AM NOT DONE
76

87
#[derive(Debug)]
98
enum Message {
109
// TODO: define the different variants used below
10+
Quit,
11+
Move { x: i32, y: i32 },
12+
Write(String),
13+
ChangeColor(i32, i32, i32),
1114
}
1215

1316
impl Message {

exercises/enums/enums3.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55
// Execute `rustlings hint enums3` or use the `hint` watch subcommand for a
66
// hint.
77

8-
// I AM NOT DONE
98

109
enum Message {
1110
// TODO: implement the message variant types based on their usage below
11+
Quit,
12+
Move { x: i32, y: i32 },
13+
Echo(String),
14+
ChangeColor(i32, i32, i32),
1215
}
1316

1417
struct Point {
@@ -20,7 +23,7 @@ struct State {
2023
color: (u8, u8, u8),
2124
position: Point,
2225
quit: bool,
23-
message: String
26+
message: String,
2427
}
2528

2629
impl State {
@@ -32,7 +35,9 @@ impl State {
3235
self.quit = true;
3336
}
3437

35-
fn echo(&mut self, s: String) { self.message = s }
38+
fn echo(&mut self, s: String) {
39+
self.message = s
40+
}
3641

3742
fn move_position(&mut self, p: Point) {
3843
self.position = p;
@@ -43,6 +48,12 @@ impl State {
4348
// variants
4449
// Remember: When passing a tuple as a function argument, you'll need
4550
// extra parentheses: fn function((t, u, p, l, e))
51+
match message {
52+
Message::Move => self.position = Move,
53+
Message::Quit => self.quit = true,
54+
Message::Echo => self.message = Echo,
55+
Message::ChangeColor => self.color = ChangeColor,
56+
}
4657
}
4758
}
4859

exercises/strings/strings1.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@
55
// Execute `rustlings hint strings1` or use the `hint` watch subcommand for a
66
// hint.
77

8-
// I AM NOT DONE
98

109
fn main() {
1110
let answer = current_favorite_color();
1211
println!("My current favorite color is {}", answer);
1312
}
1413

1514
fn current_favorite_color() -> String {
16-
"blue"
15+
String::from("blue")
1716
}

exercises/strings/strings2.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55
// Execute `rustlings hint strings2` or use the `hint` watch subcommand for a
66
// hint.
77

8-
// I AM NOT DONE
98

109
fn main() {
1110
let word = String::from("green"); // Try not changing this line :)
12-
if is_a_color_word(word) {
11+
if is_a_color_word(&word) {
1312
println!("That is a color word I know!");
1413
} else {
1514
println!("That is not a color word I know.");

exercises/strings/strings3.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,24 @@
33
// Execute `rustlings hint strings3` or use the `hint` watch subcommand for a
44
// hint.
55

6-
// I AM NOT DONE
7-
86
fn trim_me(input: &str) -> String {
97
// TODO: Remove whitespace from both ends of a string!
10-
???
8+
input.to_string();
9+
input.trim().to_string()
1110
}
1211

1312
fn compose_me(input: &str) -> String {
1413
// TODO: Add " world!" to the string! There's multiple ways to do this!
15-
???
14+
let w = String::from(" world!");
15+
let res = format!("{}{}",input,w);
16+
res
1617
}
1718

1819
fn replace_me(input: &str) -> String {
1920
// TODO: Replace "cars" in the string with "balloons"!
20-
???
21+
input.to_string();
22+
let res = input.replace("cars","balloons");
23+
res
2124
}
2225

2326
#[cfg(test)]

exercises/strings/strings4.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
//
88
// No hints this time!
99

10-
// I AM NOT DONE
1110

1211
fn string_slice(arg: &str) {
1312
println!("{}", arg);
@@ -17,14 +16,14 @@ fn string(arg: String) {
1716
}
1817

1918
fn main() {
20-
???("blue");
21-
???("red".to_string());
22-
???(String::from("hi"));
23-
???("rust is fun!".to_owned());
24-
???("nice weather".into());
25-
???(format!("Interpolation {}", "Station"));
26-
???(&String::from("abc")[0..1]);
27-
???(" hello there ".trim());
28-
???("Happy Monday!".to_string().replace("Mon", "Tues"));
29-
???("mY sHiFt KeY iS sTiCkY".to_lowercase());
19+
string_slice("blue");
20+
string("red".to_string());
21+
string(String::from("hi"));
22+
string("rust is fun!".to_owned());
23+
string("nice weather".into());
24+
string(format!("Interpolation {}", "Station"));
25+
string_slice(&String::from("abc")[0..1]);
26+
string_slice(" hello there ".trim());
27+
string("Happy Monday!".to_string().replace("Mon", "Tues"));
28+
string("mY sHiFt KeY iS sTiCkY".to_lowercase());
3029
}

exercises/structs/structs1.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
// Execute `rustlings hint structs1` or use the `hint` watch subcommand for a
66
// hint.
77

8-
// I AM NOT DONE
9-
108
struct ColorClassicStruct {
119
// TODO: Something goes here
1210
red: u8,

exercises/structs/structs2.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
// Execute `rustlings hint structs2` or use the `hint` watch subcommand for a
66
// hint.
77

8-
// I AM NOT DONE
9-
108
#[derive(Debug)]
119
struct Order {
1210
name: String,

exercises/structs/structs3.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
// Execute `rustlings hint structs3` or use the `hint` watch subcommand for a
88
// hint.
99

10-
// I AM NOT DONE
11-
1210
#[derive(Debug)]
1311
struct Package {
1412
sender_country: String,

0 commit comments

Comments
 (0)