From 3c0941fc2c87922785c362fbab6b6991d7c6d125 Mon Sep 17 00:00:00 2001 From: Suzie Kim Date: Wed, 31 Jan 2024 16:54:23 -0500 Subject: [PATCH] Complete enum exercises --- exercises/08_enums/enums1.rs | 7 +++++-- exercises/08_enums/enums2.rs | 6 ++++-- exercises/08_enums/enums3.rs | 12 ++++++++++-- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/exercises/08_enums/enums1.rs b/exercises/08_enums/enums1.rs index 25525b25..ef8200b6 100644 --- a/exercises/08_enums/enums1.rs +++ b/exercises/08_enums/enums1.rs @@ -2,13 +2,16 @@ // // No hints this time! ;) -// I AM NOT DONE - #[derive(Debug)] enum Message { // TODO: define a few types of messages as used below + Quit, + Echo, + Move, + ChangeColor } + fn main() { println!("{:?}", Message::Quit); println!("{:?}", Message::Echo); diff --git a/exercises/08_enums/enums2.rs b/exercises/08_enums/enums2.rs index df93fe0f..910b65e6 100644 --- a/exercises/08_enums/enums2.rs +++ b/exercises/08_enums/enums2.rs @@ -3,11 +3,13 @@ // Execute `rustlings hint enums2` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE - #[derive(Debug)] enum Message { // TODO: define the different variants used below + Move { x:i32, y:i32 }, + Echo(String), + ChangeColor(i32, i32, i32), + Quit, } impl Message { diff --git a/exercises/08_enums/enums3.rs b/exercises/08_enums/enums3.rs index 92d18c46..3377705b 100644 --- a/exercises/08_enums/enums3.rs +++ b/exercises/08_enums/enums3.rs @@ -5,10 +5,12 @@ // Execute `rustlings hint enums3` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE - enum Message { // TODO: implement the message variant types based on their usage below + Quit, + Echo(String), + Move(Point), + ChangeColor(u8, u8, u8), } struct Point { @@ -44,6 +46,12 @@ impl State { // 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: // fn function((t, u, p, l, e)) + match message { + Message::ChangeColor(r, g, b) => self.change_color((r, g, b)), + Message::Echo(string) => self.echo(string), + Message::Move(point) => self.move_position(point), + Message::Quit => self.quit(), + } } }