mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-11 13:19:18 +00:00
Finished enums
This commit is contained in:
parent
6e0e3dbc60
commit
892f693cfd
@ -1,10 +1,15 @@
|
||||
// enums3.rs
|
||||
// Address all the TODOs to make the tests pass!
|
||||
|
||||
// I AM NOT DONE
|
||||
// Hey, Future Justin! Come back and really dig through this. You had to use https://gist.github.com/Coutlaw/89dd16a5f7eb852c4ee44a3094bcdc09#file-enums_solution-rs,
|
||||
// But you barely hacked through this
|
||||
|
||||
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 {
|
||||
@ -37,6 +42,12 @@ impl State {
|
||||
|
||||
fn process(&mut self, message: Message) {
|
||||
// TODO: create a match expression to process the different message variants
|
||||
match message{
|
||||
Message::ChangeColor((r, g, b)) => self.change_color((r, g, b)),
|
||||
Message::Echo(s) => self.echo(s),
|
||||
Message::Move{x, y} => self.move_position(Point{x, y}),
|
||||
Message::Quit => self.quit()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -53,7 +64,7 @@ mod tests {
|
||||
};
|
||||
state.process(Message::ChangeColor((255, 0, 255)));
|
||||
state.process(Message::Echo(String::from("hello world")));
|
||||
state.process(Message::Move(Point { x: 10, y: 15 }));
|
||||
state.process(Message::Move{ x: 10, y: 15 });
|
||||
state.process(Message::Quit);
|
||||
|
||||
assert_eq!(state.color, (255, 0, 255));
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user