Finished enums

This commit is contained in:
Max Boewer 2022-12-07 23:59:40 +01:00
parent 1e2b397d6c
commit 48aa928726
3 changed files with 29 additions and 7 deletions

View File

@ -1,11 +1,14 @@
// enums1.rs // enums1.rs
// No hints this time! ;) // No hints this time! ;)
// I AM NOT DONE
#[derive(Debug)] #[derive(Debug)]
enum Message { enum Message {
// TODO: define a few types of messages as used below Quit,
Echo,
Move,
ChangeColor
} }
fn main() { fn main() {

View File

@ -1,11 +1,14 @@
// enums2.rs // enums2.rs
// Execute `rustlings hint enums2` or use the `hint` watch subcommand for a hint. // Execute `rustlings hint enums2` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
#[derive(Debug)] #[derive(Debug)]
enum Message { enum Message {
// 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

@ -2,10 +2,13 @@
// Address all the TODOs to make the tests pass! // Address all the TODOs to make the tests pass!
// Execute `rustlings hint enums3` or use the `hint` watch subcommand for a hint. // Execute `rustlings hint enums3` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
enum Message { enum Message {
// TODO: implement the message variant types based on their usage below ChangeColor((u8, u8, u8)),
Echo(String),
Move(Point),
Quit,
} }
struct Point { struct Point {
@ -37,7 +40,20 @@ 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 match message{
Message::ChangeColor(color) =>{
self.change_color(color);
}
Message::Echo(string) =>{
self.echo(string);
}
Message::Move(point) =>{
self.move_position(point);
}
Message::Quit =>{
self.quit();
}
}
} }
} }