mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-11 05:09:19 +00:00
Completed enums exercises
This commit is contained in:
parent
74e193a3b4
commit
88b29e4095
@ -1,11 +1,12 @@
|
|||||||
// 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
|
ChangeColor,
|
||||||
|
Echo,
|
||||||
|
Move,
|
||||||
|
Quit,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|||||||
@ -1,11 +1,12 @@
|
|||||||
// 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
|
ChangeColor(u8, u8, u8),
|
||||||
|
Echo(String),
|
||||||
|
Move { x: i32, y: i32 },
|
||||||
|
Quit,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Message {
|
impl Message {
|
||||||
|
|||||||
@ -2,10 +2,11 @@
|
|||||||
// 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,8 +38,13 @@ 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
|
|
||||||
// Remember: When passing a tuple as a function argument, you'll need extra parentheses: fn function((t, u, p, l, e))
|
// Remember: When passing a tuple as a function argument, you'll need extra parentheses: fn function((t, u, p, l, e))
|
||||||
|
match message {
|
||||||
|
Message::ChangeColor(r, g, b) => self.change_color((r, g, b)),
|
||||||
|
Message::Echo(msg) => self.echo(msg),
|
||||||
|
Message::Move(p) => self.move_position(p),
|
||||||
|
Message::Quit => self.quit(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user