Enum exercises complete

This commit is contained in:
Emre AYDIN 2021-01-05 17:07:42 +03:00
parent 61c3989ffb
commit c328bd6f5e
3 changed files with 18 additions and 6 deletions

View file

@ -1,11 +1,13 @@
// enums1.rs // enums1.rs
// Make me compile! Execute `rustlings hint enums1` for hints! // Make me compile! Execute `rustlings hint enums1` for hints!
// I AM NOT DONE
#[derive(Debug)] #[derive(Debug)]
enum Message { enum Message {
// TODO: define a few types of messages as used below // TODO: define a few types of messages as used below
Quit,
Echo,
Move,
ChangeColor,
} }
fn main() { fn main() {

View file

@ -1,11 +1,13 @@
// enums2.rs // enums2.rs
// Make me compile! Execute `rustlings hint enums2` for hints! // Make me compile! Execute `rustlings hint enums2` for hints!
// I AM NOT DONE
#[derive(Debug)] #[derive(Debug)]
enum Message { enum Message {
// TODO: define the different variants used below // TODO: define the different variants used below
Move { x: i32, y: i32 },
Echo(String),
ChangeColor(i32, i32, i32),
Quit,
} }
impl Message { impl Message {

View file

@ -1,10 +1,12 @@
// enums3.rs // enums3.rs
// Address all the TODOs to make the tests pass! // Address all the TODOs to make the tests pass!
// I AM NOT DONE
enum Message { enum Message {
// TODO: implement the message variant types based on their usage below // TODO: implement the message variant types based on their usage below
Move(Point),
Echo(String),
ChangeColor((u8, u8, u8)),
Quit,
} }
struct Point { struct Point {
@ -37,6 +39,12 @@ impl State {
fn process(&mut self, message: Message) { fn process(&mut self, message: Message) {
// TODO: create a match expression to process the different message variants // TODO: create a match expression to process the different message variants
match message {
Message::Move(point) => self.move_position(point),
Message::ChangeColor(color) => self.change_color(color),
Message::Quit => self.quit(),
Message::Echo(s) => self.echo(s),
};
} }
} }