finished enums

This commit is contained in:
Jack Harper 2021-09-25 12:01:56 +01:00
parent cf46c715c3
commit 3ffef7bbb4
3 changed files with 21 additions and 9 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
Quit,
Echo(String),
Move{x:i32, y:i32},
ChangeColor(u8, u8, u8)
}
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
Quit,
Echo(String),
Move(Point),
ChangeColor((u8, u8, u8))
}
struct Point {
@ -36,6 +37,15 @@ impl State {
}
fn process(&mut self, message: Message) {
match message{
Message::Quit => self.quit(),
Message::Move(p) => { self.move_position(p) },
Message::Echo(s) => self.echo(s),
Message::ChangeColor((r, g, b)) => {
self.change_color((r, g, b));
}
}
// TODO: create a match expression to process the different message variants
}
}