Solve enums

This commit is contained in:
Manuel Gil 2020-03-02 09:29:54 +00:00
parent 99a3c200e0
commit a1123e4f25
3 changed files with 18 additions and 10 deletions

View file

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

View file

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

View file

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