mirror of
https://github.com/rust-lang/rustlings.git
synced 2024-12-27 00:00:03 +03:00
Complete enum exercises
This commit is contained in:
parent
4b14488a4b
commit
3c0941fc2c
|
@ -2,13 +2,16 @@
|
||||||
//
|
//
|
||||||
// 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
|
// TODO: define a few types of messages as used below
|
||||||
|
Quit,
|
||||||
|
Echo,
|
||||||
|
Move,
|
||||||
|
ChangeColor
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("{:?}", Message::Quit);
|
println!("{:?}", Message::Quit);
|
||||||
println!("{:?}", Message::Echo);
|
println!("{:?}", Message::Echo);
|
||||||
|
|
|
@ -3,11 +3,13 @@
|
||||||
// Execute `rustlings hint enums2` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint enums2` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
enum Message {
|
enum Message {
|
||||||
// TODO: define the different variants used below
|
// TODO: define the different variants used below
|
||||||
|
Move { x:i32, y:i32 },
|
||||||
|
Echo(String),
|
||||||
|
ChangeColor(i32, i32, i32),
|
||||||
|
Quit,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Message {
|
impl Message {
|
||||||
|
|
|
@ -5,10 +5,12 @@
|
||||||
// Execute `rustlings hint enums3` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint enums3` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
enum Message {
|
enum Message {
|
||||||
// TODO: implement the message variant types based on their usage below
|
// TODO: implement the message variant types based on their usage below
|
||||||
|
Quit,
|
||||||
|
Echo(String),
|
||||||
|
Move(Point),
|
||||||
|
ChangeColor(u8, u8, u8),
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Point {
|
struct Point {
|
||||||
|
@ -44,6 +46,12 @@ impl State {
|
||||||
// TODO: create a match expression to process the different message variants
|
// 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:
|
// Remember: When passing a tuple as a function argument, you'll need extra parentheses:
|
||||||
// fn function((t, u, p, l, e))
|
// fn function((t, u, p, l, e))
|
||||||
|
match message {
|
||||||
|
Message::ChangeColor(r, g, b) => self.change_color((r, g, b)),
|
||||||
|
Message::Echo(string) => self.echo(string),
|
||||||
|
Message::Move(point) => self.move_position(point),
|
||||||
|
Message::Quit => self.quit(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue