mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-09 12:19:18 +00:00
Solve enum exercises.
This commit is contained in:
parent
2a12b63207
commit
e730260ad4
@ -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
|
||||
Quit,
|
||||
Echo(String),
|
||||
Move {x: i32, y: i32},
|
||||
ChangeColor(i32, i32, i32),
|
||||
}
|
||||
|
||||
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
|
||||
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")));
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user