Skip to content

Commit 67d8d58

Browse files
authored
Merge pull request #1774 from matthri/fix-enum-variant-inconsistency
Make enum variants more consistent between exercises
2 parents 4d9c346 + 43d15f0 commit 67d8d58

File tree

4 files changed

+22
-2
lines changed

4 files changed

+22
-2
lines changed

exercises/08_enums/enums1.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ enum Message {
66
fn main() {
77
println!("{:?}", Message::Quit);
88
println!("{:?}", Message::Echo);
9+
println!("{:?}", Message::Resize);
910
println!("{:?}", Message::Move);
1011
println!("{:?}", Message::ChangeColor);
1112
}

exercises/08_enums/enums2.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ enum Message {
44
// TODO: Define the different variants used below.
55
}
66

7+
#[derive(Debug)]
8+
struct Point {
9+
x: u8,
10+
y: u8,
11+
}
12+
713
impl Message {
814
fn call(&self) {
915
println!("{self:?}");
@@ -12,7 +18,8 @@ impl Message {
1218

1319
fn main() {
1420
let messages = [
15-
Message::Move { x: 10, y: 30 },
21+
Message::Resize { w: 10, h: 30 },
22+
Message::Move(Point { x: 10, y: 15 }),
1623
Message::Echo(String::from("hello world")),
1724
Message::ChangeColor(200, 255, 255),
1825
Message::Quit,

exercises/08_enums/enums3.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ struct Point {
99

1010
struct State {
1111
color: (u8, u8, u8),
12+
width: u8,
13+
height: u8,
1214
position: Point,
1315
quit: bool,
1416
message: String,
@@ -27,6 +29,11 @@ impl State {
2729
self.message = s;
2830
}
2931

32+
fn resize(&mut self, width: u8, height: u8) {
33+
self.width = width;
34+
self.height = height;
35+
}
36+
3037
fn move_position(&mut self, point: Point) {
3138
self.position = point;
3239
}
@@ -50,17 +57,22 @@ mod tests {
5057
fn test_match_message_call() {
5158
let mut state = State {
5259
quit: false,
60+
width: 0,
61+
height: 0,
5362
position: Point { x: 0, y: 0 },
5463
color: (0, 0, 0),
5564
message: String::from("hello world"),
5665
};
5766

5867
state.process(Message::ChangeColor(255, 0, 255));
5968
state.process(Message::Echo(String::from("Hello world!")));
69+
state.process(Message::Resize { w: 10, h: 30 });
6070
state.process(Message::Move(Point { x: 10, y: 15 }));
6171
state.process(Message::Quit);
6272

6373
assert_eq!(state.color, (255, 0, 255));
74+
assert_eq!(state.width, 10);
75+
assert_eq!(state.height, 30);
6476
assert_eq!(state.position.x, 10);
6577
assert_eq!(state.position.y, 15);
6678
assert!(state.quit);

rustlings-macros/info.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ dir = "08_enums"
445445
test = false
446446
hint = """
447447
You can create enumerations that have different variants with different types
448-
such as no data, anonymous structs, a single string, tuples, etc."""
448+
such as anonymous structs, structs, a single string, tuples, no data, etc."""
449449

450450
[[exercises]]
451451
name = "enums3"

0 commit comments

Comments
 (0)