Skip to content

Commit 4c4398e

Browse files
feat: Enums
1 parent 9c7e768 commit 4c4398e

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

rustlings/exercises/enums/enums1.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
//
33
// No hints this time! ;)
44

5-
// I AM NOT DONE
6-
75
#[derive(Debug)]
86
enum Message {
9-
// TODO: define a few types of messages as used below
7+
Quit,
8+
Echo,
9+
Move,
10+
ChangeColor,
1011
}
1112

1213
fn main() {

rustlings/exercises/enums/enums2.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
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 {
10-
// TODO: define the different variants used below
9+
Move {x: i32, y: i32},
10+
Echo(String),
11+
ChangeColor(u8,u8,u8),
12+
Quit,
1113
}
1214

1315
impl Message {

rustlings/exercises/enums/enums3.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
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 {
11-
// TODO: implement the message variant types based on their usage below
10+
ChangeColor(u8, u8, u8),
11+
Echo(String),
12+
Move(Point),
13+
Quit,
1214
}
1315

1416
struct Point {
@@ -41,10 +43,13 @@ impl State {
4143
}
4244

4345
fn process(&mut self, message: Message) {
44-
// TODO: create a match expression to process the different message
45-
// variants
46-
// Remember: When passing a tuple as a function argument, you'll need
47-
// extra parentheses: fn function((t, u, p, l, e))
46+
match message {
47+
Message::ChangeColor(red,black,green) => self.change_color((red,black,green)),
48+
Message::Quit => self.quit(),
49+
Message::Echo(text) => self.echo(text),
50+
Message::Move(point) => self.move_position(point),
51+
Message::Quit => self.quit(),
52+
}
4853
}
4954
}
5055

0 commit comments

Comments
 (0)