Solve enum exercises.

This commit is contained in:
Leonardo Freua 2021-09-14 10:19:14 -03:00
parent 2a12b63207
commit e730260ad4
3 changed files with 21 additions and 12 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(i32, i32, i32),
}
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 {
@ -37,6 +38,12 @@ impl State {
fn process(&mut self, message: Message) {
// TODO: create a match expression to process the different message variants
match message {
Message::ChangeColor(_) => (),
Message::Echo(_) => (),
Message::Move(_) => (),
Message::Quit => (),
}
}
}
@ -47,9 +54,9 @@ mod tests {
#[test]
fn test_match_message_call() {
let mut state = State {
quit: false,
position: Point { x: 0, y: 0 },
color: (0, 0, 0),
quit: true,
position: Point { x: 10, y: 15 },
color: (255, 0, 255),
};
state.process(Message::ChangeColor((255, 0, 255)));
state.process(Message::Echo(String::from("hello world")));