Skip to content

Commit 584164a

Browse files
committed
Adjust enums exercises
1 parent e6f6d26 commit 584164a

File tree

6 files changed

+102
-64
lines changed

6 files changed

+102
-64
lines changed

exercises/08_enums/enums1.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ enum Message {
44
}
55

66
fn main() {
7-
println!("{:?}", Message::Quit);
8-
println!("{:?}", Message::Echo);
97
println!("{:?}", Message::Resize);
108
println!("{:?}", Message::Move);
9+
println!("{:?}", Message::Echo);
1110
println!("{:?}", Message::ChangeColor);
11+
println!("{:?}", Message::Quit);
1212
}

exercises/08_enums/enums2.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
#[allow(dead_code)]
1+
#![allow(dead_code)]
2+
23
#[derive(Debug)]
3-
enum Message {
4-
// TODO: Define the different variants used below.
4+
struct Point {
5+
x: u64,
6+
y: u64,
57
}
68

79
#[derive(Debug)]
8-
struct Point {
9-
x: u8,
10-
y: u8,
10+
enum Message {
11+
// TODO: Define the different variants used below.
1112
}
1213

1314
impl Message {
@@ -18,7 +19,10 @@ impl Message {
1819

1920
fn main() {
2021
let messages = [
21-
Message::Resize { w: 10, h: 30 },
22+
Message::Resize {
23+
width: 10,
24+
height: 30,
25+
},
2226
Message::Move(Point { x: 10, y: 15 }),
2327
Message::Echo(String::from("hello world")),
2428
Message::ChangeColor(200, 255, 255),

exercises/08_enums/enums3.rs

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,41 @@
1-
enum Message {
2-
// TODO: Implement the message variant types based on their usage below.
1+
struct Point {
2+
x: u64,
3+
y: u64,
34
}
45

5-
struct Point {
6-
x: u8,
7-
y: u8,
6+
enum Message {
7+
// TODO: Implement the message variant types based on their usage below.
88
}
99

1010
struct State {
11-
color: (u8, u8, u8),
12-
width: u8,
13-
height: u8,
11+
width: u64,
12+
height: u64,
1413
position: Point,
15-
quit: bool,
1614
message: String,
15+
color: (u8, u8, u8),
16+
quit: bool,
1717
}
1818

1919
impl State {
20-
fn change_color(&mut self, color: (u8, u8, u8)) {
21-
self.color = color;
20+
fn resize(&mut self, width: u64, height: u64) {
21+
self.width = width;
22+
self.height = height;
2223
}
2324

24-
fn quit(&mut self) {
25-
self.quit = true;
25+
fn move_position(&mut self, point: Point) {
26+
self.position = point;
2627
}
2728

2829
fn echo(&mut self, s: String) {
2930
self.message = s;
3031
}
3132

32-
fn resize(&mut self, width: u8, height: u8) {
33-
self.width = width;
34-
self.height = height;
33+
fn change_color(&mut self, color: (u8, u8, u8)) {
34+
self.color = color;
3535
}
3636

37-
fn move_position(&mut self, point: Point) {
38-
self.position = point;
37+
fn quit(&mut self) {
38+
self.quit = true;
3939
}
4040

4141
fn process(&mut self, message: Message) {
@@ -56,26 +56,29 @@ mod tests {
5656
#[test]
5757
fn test_match_message_call() {
5858
let mut state = State {
59-
quit: false,
6059
width: 0,
6160
height: 0,
6261
position: Point { x: 0, y: 0 },
63-
color: (0, 0, 0),
6462
message: String::from("hello world"),
63+
color: (0, 0, 0),
64+
quit: false,
6565
};
6666

67-
state.process(Message::ChangeColor(255, 0, 255));
68-
state.process(Message::Echo(String::from("Hello world!")));
69-
state.process(Message::Resize { w: 10, h: 30 });
67+
state.process(Message::Resize {
68+
width: 10,
69+
height: 30,
70+
});
7071
state.process(Message::Move(Point { x: 10, y: 15 }));
72+
state.process(Message::Echo(String::from("Hello world!")));
73+
state.process(Message::ChangeColor(255, 0, 255));
7174
state.process(Message::Quit);
7275

73-
assert_eq!(state.color, (255, 0, 255));
7476
assert_eq!(state.width, 10);
7577
assert_eq!(state.height, 30);
7678
assert_eq!(state.position.x, 10);
7779
assert_eq!(state.position.y, 15);
78-
assert!(state.quit);
7980
assert_eq!(state.message, "Hello world!");
81+
assert_eq!(state.color, (255, 0, 255));
82+
assert!(state.quit);
8083
}
8184
}

solutions/08_enums/enums1.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
#[derive(Debug)]
22
enum Message {
3-
Quit,
4-
Echo,
3+
Resize,
54
Move,
5+
Echo,
66
ChangeColor,
7+
Quit,
78
}
89

910
fn main() {
10-
println!("{:?}", Message::Quit);
11-
println!("{:?}", Message::Echo);
11+
println!("{:?}", Message::Resize);
1212
println!("{:?}", Message::Move);
13+
println!("{:?}", Message::Echo);
1314
println!("{:?}", Message::ChangeColor);
15+
println!("{:?}", Message::Quit);
1416
}

solutions/08_enums/enums2.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1-
#[allow(dead_code)]
1+
#![allow(dead_code)]
2+
3+
#[derive(Debug)]
4+
struct Point {
5+
x: u64,
6+
y: u64,
7+
}
8+
29
#[derive(Debug)]
310
enum Message {
4-
Move { x: i64, y: i64 },
11+
Resize { width: u64, height: u64 },
12+
Move(Point),
513
Echo(String),
614
ChangeColor(u8, u8, u8),
715
Quit,
@@ -15,7 +23,11 @@ impl Message {
1523

1624
fn main() {
1725
let messages = [
18-
Message::Move { x: 10, y: 30 },
26+
Message::Resize {
27+
width: 10,
28+
height: 30,
29+
},
30+
Message::Move(Point { x: 10, y: 15 }),
1931
Message::Echo(String::from("hello world")),
2032
Message::ChangeColor(200, 255, 255),
2133
Message::Quit,

solutions/08_enums/enums3.rs

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,53 @@
1+
struct Point {
2+
x: u64,
3+
y: u64,
4+
}
5+
16
enum Message {
2-
ChangeColor(u8, u8, u8),
3-
Echo(String),
7+
Resize { width: u64, height: u64 },
48
Move(Point),
9+
Echo(String),
10+
ChangeColor(u8, u8, u8),
511
Quit,
612
}
713

8-
struct Point {
9-
x: u8,
10-
y: u8,
11-
}
12-
1314
struct State {
14-
color: (u8, u8, u8),
15+
width: u64,
16+
height: u64,
1517
position: Point,
16-
quit: bool,
1718
message: String,
19+
color: (u8, u8, u8),
20+
quit: bool,
1821
}
1922

2023
impl State {
21-
fn change_color(&mut self, color: (u8, u8, u8)) {
22-
self.color = color;
24+
fn resize(&mut self, width: u64, height: u64) {
25+
self.width = width;
26+
self.height = height;
2327
}
2428

25-
fn quit(&mut self) {
26-
self.quit = true;
29+
fn move_position(&mut self, point: Point) {
30+
self.position = point;
2731
}
2832

2933
fn echo(&mut self, s: String) {
3034
self.message = s;
3135
}
3236

33-
fn move_position(&mut self, point: Point) {
34-
self.position = point;
37+
fn change_color(&mut self, color: (u8, u8, u8)) {
38+
self.color = color;
39+
}
40+
41+
fn quit(&mut self) {
42+
self.quit = true;
3543
}
3644

3745
fn process(&mut self, message: Message) {
3846
match message {
39-
Message::ChangeColor(r, g, b) => self.change_color((r, g, b)),
40-
Message::Echo(s) => self.echo(s),
47+
Message::Resize { width, height } => self.resize(width, height),
4148
Message::Move(point) => self.move_position(point),
49+
Message::Echo(s) => self.echo(s),
50+
Message::ChangeColor(r, g, b) => self.change_color((r, g, b)),
4251
Message::Quit => self.quit(),
4352
}
4453
}
@@ -55,21 +64,29 @@ mod tests {
5564
#[test]
5665
fn test_match_message_call() {
5766
let mut state = State {
58-
quit: false,
67+
width: 0,
68+
height: 0,
5969
position: Point { x: 0, y: 0 },
60-
color: (0, 0, 0),
6170
message: String::from("hello world"),
71+
color: (0, 0, 0),
72+
quit: false,
6273
};
6374

64-
state.process(Message::ChangeColor(255, 0, 255));
65-
state.process(Message::Echo(String::from("Hello world!")));
75+
state.process(Message::Resize {
76+
width: 10,
77+
height: 30,
78+
});
6679
state.process(Message::Move(Point { x: 10, y: 15 }));
80+
state.process(Message::Echo(String::from("Hello world!")));
81+
state.process(Message::ChangeColor(255, 0, 255));
6782
state.process(Message::Quit);
6883

69-
assert_eq!(state.color, (255, 0, 255));
84+
assert_eq!(state.width, 10);
85+
assert_eq!(state.height, 30);
7086
assert_eq!(state.position.x, 10);
7187
assert_eq!(state.position.y, 15);
72-
assert!(state.quit);
7388
assert_eq!(state.message, "Hello world!");
89+
assert_eq!(state.color, (255, 0, 255));
90+
assert!(state.quit);
7491
}
7592
}

0 commit comments

Comments
 (0)