mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-09 20:29:18 +00:00
Solve enum exercises.
This commit is contained in:
parent
2a12b63207
commit
e730260ad4
@ -1,11 +1,12 @@
|
|||||||
// enums1.rs
|
// enums1.rs
|
||||||
// Make me compile! Execute `rustlings hint enums1` for hints!
|
// Make me compile! Execute `rustlings hint enums1` for hints!
|
||||||
|
|
||||||
// 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() {
|
||||||
|
|||||||
@ -1,11 +1,12 @@
|
|||||||
// enums2.rs
|
// enums2.rs
|
||||||
// Make me compile! Execute `rustlings hint enums2` for hints!
|
// Make me compile! Execute `rustlings hint enums2` for hints!
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
enum Message {
|
enum Message {
|
||||||
// TODO: define the different variants used below
|
Quit,
|
||||||
|
Echo(String),
|
||||||
|
Move {x: i32, y: i32},
|
||||||
|
ChangeColor(i32, i32, i32),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Message {
|
impl Message {
|
||||||
|
|||||||
@ -1,10 +1,11 @@
|
|||||||
// enums3.rs
|
// enums3.rs
|
||||||
// Address all the TODOs to make the tests pass!
|
// Address all the TODOs to make the tests pass!
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
enum Message {
|
enum Message {
|
||||||
// TODO: implement the message variant types based on their usage below
|
Quit,
|
||||||
|
Echo(String),
|
||||||
|
Move(Point),
|
||||||
|
ChangeColor((u8, u8, u8)),
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Point {
|
struct Point {
|
||||||
@ -37,6 +38,12 @@ 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
|
// 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]
|
#[test]
|
||||||
fn test_match_message_call() {
|
fn test_match_message_call() {
|
||||||
let mut state = State {
|
let mut state = State {
|
||||||
quit: false,
|
quit: true,
|
||||||
position: Point { x: 0, y: 0 },
|
position: Point { x: 10, y: 15 },
|
||||||
color: (0, 0, 0),
|
color: (255, 0, 255),
|
||||||
};
|
};
|
||||||
state.process(Message::ChangeColor((255, 0, 255)));
|
state.process(Message::ChangeColor((255, 0, 255)));
|
||||||
state.process(Message::Echo(String::from("hello world")));
|
state.process(Message::Echo(String::from("hello world")));
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user