diff --git a/exercises/08_enums/enums1.rs b/exercises/08_enums/enums1.rs index c65e641a..c0d0c308 100644 --- a/exercises/08_enums/enums1.rs +++ b/exercises/08_enums/enums1.rs @@ -4,9 +4,9 @@ enum Message { } fn main() { - println!("{:?}", Message::Quit); - println!("{:?}", Message::Echo); println!("{:?}", Message::Resize); println!("{:?}", Message::Move); + println!("{:?}", Message::Echo); println!("{:?}", Message::ChangeColor); + println!("{:?}", Message::Quit); } diff --git a/exercises/08_enums/enums2.rs b/exercises/08_enums/enums2.rs index 5a74991a..29ed1b6f 100644 --- a/exercises/08_enums/enums2.rs +++ b/exercises/08_enums/enums2.rs @@ -1,13 +1,14 @@ -#[allow(dead_code)] -#[derive(Debug)] -enum Message { - // TODO: Define the different variants used below. -} +#![allow(dead_code)] #[derive(Debug)] struct Point { - x: u8, - y: u8, + x: u64, + y: u64, +} + +#[derive(Debug)] +enum Message { + // TODO: Define the different variants used below. } impl Message { @@ -18,7 +19,10 @@ impl Message { fn main() { let messages = [ - Message::Resize { w: 10, h: 30 }, + Message::Resize { + width: 10, + height: 30, + }, Message::Move(Point { x: 10, y: 15 }), Message::Echo(String::from("hello world")), Message::ChangeColor(200, 255, 255), diff --git a/exercises/08_enums/enums3.rs b/exercises/08_enums/enums3.rs index 2d0d82d4..f49707ca 100644 --- a/exercises/08_enums/enums3.rs +++ b/exercises/08_enums/enums3.rs @@ -1,35 +1,23 @@ +struct Point { + x: u64, + y: u64, +} + enum Message { // TODO: Implement the message variant types based on their usage below. } -struct Point { - x: u8, - y: u8, -} - struct State { - color: (u8, u8, u8), - width: u8, - height: u8, + width: u64, + height: u64, position: Point, - quit: bool, message: String, + color: (u8, u8, u8), + quit: bool, } impl State { - fn change_color(&mut self, color: (u8, u8, u8)) { - self.color = color; - } - - fn quit(&mut self) { - self.quit = true; - } - - fn echo(&mut self, s: String) { - self.message = s; - } - - fn resize(&mut self, width: u8, height: u8) { + fn resize(&mut self, width: u64, height: u64) { self.width = width; self.height = height; } @@ -38,6 +26,18 @@ impl State { self.position = point; } + fn echo(&mut self, s: String) { + self.message = s; + } + + fn change_color(&mut self, color: (u8, u8, u8)) { + self.color = color; + } + + fn quit(&mut self) { + self.quit = true; + } + fn process(&mut self, message: Message) { // TODO: Create a match expression to process the different message variants. // Remember: When passing a tuple as a function argument, you'll need extra parentheses: @@ -56,26 +56,29 @@ mod tests { #[test] fn test_match_message_call() { let mut state = State { - quit: false, width: 0, height: 0, position: Point { x: 0, y: 0 }, - color: (0, 0, 0), message: String::from("hello world"), + color: (0, 0, 0), + quit: false, }; - state.process(Message::ChangeColor(255, 0, 255)); - state.process(Message::Echo(String::from("Hello world!"))); - state.process(Message::Resize { w: 10, h: 30 }); + state.process(Message::Resize { + width: 10, + height: 30, + }); state.process(Message::Move(Point { x: 10, y: 15 })); + state.process(Message::Echo(String::from("Hello world!"))); + state.process(Message::ChangeColor(255, 0, 255)); state.process(Message::Quit); - assert_eq!(state.color, (255, 0, 255)); assert_eq!(state.width, 10); assert_eq!(state.height, 30); assert_eq!(state.position.x, 10); assert_eq!(state.position.y, 15); - assert!(state.quit); assert_eq!(state.message, "Hello world!"); + assert_eq!(state.color, (255, 0, 255)); + assert!(state.quit); } } diff --git a/solutions/08_enums/enums1.rs b/solutions/08_enums/enums1.rs index 97248834..97a5cc06 100644 --- a/solutions/08_enums/enums1.rs +++ b/solutions/08_enums/enums1.rs @@ -1,14 +1,16 @@ #[derive(Debug)] enum Message { - Quit, - Echo, + Resize, Move, + Echo, ChangeColor, + Quit, } fn main() { - println!("{:?}", Message::Quit); - println!("{:?}", Message::Echo); + println!("{:?}", Message::Resize); println!("{:?}", Message::Move); + println!("{:?}", Message::Echo); println!("{:?}", Message::ChangeColor); + println!("{:?}", Message::Quit); } diff --git a/solutions/08_enums/enums2.rs b/solutions/08_enums/enums2.rs index b19394c6..2ee0553a 100644 --- a/solutions/08_enums/enums2.rs +++ b/solutions/08_enums/enums2.rs @@ -1,7 +1,15 @@ -#[allow(dead_code)] +#![allow(dead_code)] + +#[derive(Debug)] +struct Point { + x: u64, + y: u64, +} + #[derive(Debug)] enum Message { - Move { x: i64, y: i64 }, + Resize { width: u64, height: u64 }, + Move(Point), Echo(String), ChangeColor(u8, u8, u8), Quit, @@ -15,7 +23,11 @@ impl Message { fn main() { let messages = [ - Message::Move { x: 10, y: 30 }, + Message::Resize { + width: 10, + height: 30, + }, + Message::Move(Point { x: 10, y: 15 }), Message::Echo(String::from("hello world")), Message::ChangeColor(200, 255, 255), Message::Quit, diff --git a/solutions/08_enums/enums3.rs b/solutions/08_enums/enums3.rs index 8baa25c1..8641fbdb 100644 --- a/solutions/08_enums/enums3.rs +++ b/solutions/08_enums/enums3.rs @@ -1,23 +1,39 @@ +struct Point { + x: u64, + y: u64, +} + enum Message { - ChangeColor(u8, u8, u8), - Echo(String), + Resize { width: u64, height: u64 }, Move(Point), + Echo(String), + ChangeColor(u8, u8, u8), Quit, } -struct Point { - x: u8, - y: u8, -} - struct State { - color: (u8, u8, u8), + width: u64, + height: u64, position: Point, - quit: bool, message: String, + color: (u8, u8, u8), + quit: bool, } impl State { + fn resize(&mut self, width: u64, height: u64) { + self.width = width; + self.height = height; + } + + fn move_position(&mut self, point: Point) { + self.position = point; + } + + fn echo(&mut self, s: String) { + self.message = s; + } + fn change_color(&mut self, color: (u8, u8, u8)) { self.color = color; } @@ -26,19 +42,12 @@ impl State { self.quit = true; } - fn echo(&mut self, s: String) { - self.message = s; - } - - fn move_position(&mut self, point: Point) { - self.position = point; - } - fn process(&mut self, message: Message) { match message { - Message::ChangeColor(r, g, b) => self.change_color((r, g, b)), - Message::Echo(s) => self.echo(s), + Message::Resize { width, height } => self.resize(width, height), Message::Move(point) => self.move_position(point), + Message::Echo(s) => self.echo(s), + Message::ChangeColor(r, g, b) => self.change_color((r, g, b)), Message::Quit => self.quit(), } } @@ -55,21 +64,29 @@ mod tests { #[test] fn test_match_message_call() { let mut state = State { - quit: false, + width: 0, + height: 0, position: Point { x: 0, y: 0 }, - color: (0, 0, 0), message: String::from("hello world"), + color: (0, 0, 0), + quit: false, }; - state.process(Message::ChangeColor(255, 0, 255)); - state.process(Message::Echo(String::from("Hello world!"))); + state.process(Message::Resize { + width: 10, + height: 30, + }); state.process(Message::Move(Point { x: 10, y: 15 })); + state.process(Message::Echo(String::from("Hello world!"))); + state.process(Message::ChangeColor(255, 0, 255)); state.process(Message::Quit); - assert_eq!(state.color, (255, 0, 255)); + assert_eq!(state.width, 10); + assert_eq!(state.height, 30); assert_eq!(state.position.x, 10); assert_eq!(state.position.y, 15); - assert!(state.quit); assert_eq!(state.message, "Hello world!"); + assert_eq!(state.color, (255, 0, 255)); + assert!(state.quit); } }