mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-11 21:29:18 +00:00
[Add] exercises/enums
This commit is contained in:
parent
21ec90c3c7
commit
e9569b0a49
@ -1,11 +1,12 @@
|
||||
// enums1.rs
|
||||
// No hints this time! ;)
|
||||
|
||||
// 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
|
||||
// Execute `rustlings hint enums2` or use the `hint` watch subcommand for a hint.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
#[derive(Debug)]
|
||||
enum Message {
|
||||
// TODO: define the different variants used below
|
||||
Quit,
|
||||
Echo(String),
|
||||
ChangeColor(isize, isize, isize),
|
||||
Move { x: isize, y: isize },
|
||||
}
|
||||
|
||||
impl Message {
|
||||
|
||||
@ -2,10 +2,11 @@
|
||||
// Address all the TODOs to make the tests pass!
|
||||
// Execute `rustlings hint enums3` or use the `hint` watch subcommand for a hint.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
enum Message {
|
||||
// TODO: implement the message variant types based on their usage below
|
||||
Quit,
|
||||
Move(Point),
|
||||
Echo(String),
|
||||
ChangeColor(u8, u8, u8),
|
||||
}
|
||||
|
||||
struct Point {
|
||||
@ -37,8 +38,12 @@ impl State {
|
||||
}
|
||||
|
||||
fn process(&mut self, message: Message) {
|
||||
// TODO: create a match expression to process the different message variants
|
||||
// Remember: When passing a tuple as a function argument, you'll need extra parentheses: fn function((t, u, p, l, e))
|
||||
match message {
|
||||
Message::Quit => self.quit = true,
|
||||
Message::Move(point) => self.position = point,
|
||||
Message::Echo(_) => {},
|
||||
Message::ChangeColor(r, g, b) => self.color = (r, g, b),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user