Skip to content

Commit 77b687d

Browse files
committed
fix(enums) make enum variants more consistent
closes #1545
1 parent bc3808c commit 77b687d

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
@@ -12,6 +12,7 @@ enum Message {
1212
fn main() {
1313
println!("{:?}", Message::Quit);
1414
println!("{:?}", Message::Echo);
15+
println!("{:?}", Message::Resize);
1516
println!("{:?}", Message::Move);
1617
println!("{:?}", Message::ChangeColor);
1718
}

exercises/08_enums/enums2.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ enum Message {
1010
// TODO: define the different variants used below
1111
}
1212

13+
#[derive(Debug)]
14+
struct Point {
15+
x: u8,
16+
y: u8,
17+
}
18+
1319
impl Message {
1420
fn call(&self) {
1521
println!("{:?}", self);
@@ -18,7 +24,8 @@ impl Message {
1824

1925
fn main() {
2026
let messages = [
21-
Message::Move { x: 10, y: 30 },
27+
Message::Resize { w: 10, h: 30 },
28+
Message::Move(Point { x: 10, y: 15 }),
2229
Message::Echo(String::from("hello world")),
2330
Message::ChangeColor(200, 255, 255),
2431
Message::Quit,

exercises/08_enums/enums3.rs

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

1919
struct State {
2020
color: (u8, u8, u8),
21+
width: u8,
22+
height: u8,
2123
position: Point,
2224
quit: bool,
2325
message: String,
@@ -36,6 +38,11 @@ impl State {
3638
self.message = s
3739
}
3840

41+
fn resize(&mut self, w: u8, h: u8) {
42+
self.width = w;
43+
self.height = h;
44+
}
45+
3946
fn move_position(&mut self, p: Point) {
4047
self.position = p;
4148
}
@@ -55,16 +62,21 @@ mod tests {
5562
fn test_match_message_call() {
5663
let mut state = State {
5764
quit: false,
65+
width: 0,
66+
height: 0,
5867
position: Point { x: 0, y: 0 },
5968
color: (0, 0, 0),
6069
message: "hello world".to_string(),
6170
};
6271
state.process(Message::ChangeColor(255, 0, 255));
6372
state.process(Message::Echo(String::from("Hello world!")));
73+
state.process(Message::Resize { w: 10, h: 30 });
6474
state.process(Message::Move(Point { x: 10, y: 15 }));
6575
state.process(Message::Quit);
6676

6777
assert_eq!(state.color, (255, 0, 255));
78+
assert_eq!(state.width, 10);
79+
assert_eq!(state.height, 30);
6880
assert_eq!(state.position.x, 10);
6981
assert_eq!(state.position.y, 15);
7082
assert_eq!(state.quit, true);

info.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ path = "exercises/08_enums/enums2.rs"
468468
mode = "compile"
469469
hint = """
470470
You can create enumerations that have different variants with different types
471-
such as no data, anonymous structs, a single string, tuples, ...etc"""
471+
such as anonymous structs, structs, a single string, tuples, no data, ...etc"""
472472

473473
[[exercises]]
474474
name = "enums3"

0 commit comments

Comments
 (0)