Complete enum exercises (#7)
This commit is contained in:
parent
b48abc99a1
commit
5c0a53bfb3
|
@ -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() {
|
||||
|
|
|
@ -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: i32, y: i32},
|
||||
Echo(String),
|
||||
ChangeColor(i32, i32, i32),
|
||||
Quit,
|
||||
}
|
||||
|
||||
impl Message {
|
||||
|
|
|
@ -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
|
||||
Move(Point),
|
||||
Echo(String),
|
||||
ChangeColor((u8, u8, 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::Move(p) => self.move_position(p),
|
||||
Message::Echo(s) => self.echo(s),
|
||||
Message::ChangeColor(rgb) => self.change_color(rgb),
|
||||
Message::Quit => self.quit(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue