mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-09 20:29:18 +00:00
enum
This commit is contained in:
parent
a2c4ec5cce
commit
239e33f221
@ -2,11 +2,13 @@
|
||||
//
|
||||
// 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() {
|
||||
|
||||
@ -3,11 +3,13 @@
|
||||
// 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
|
||||
Move {x: i32, y: i32},
|
||||
Echo(String),
|
||||
ChangeColor(i32, i32, i32),
|
||||
Quit,
|
||||
}
|
||||
|
||||
impl Message {
|
||||
|
||||
@ -5,10 +5,12 @@
|
||||
// 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
|
||||
Move(Point),
|
||||
Echo(String),
|
||||
ChangeColor((u8, u8, u8)),
|
||||
Quit,
|
||||
}
|
||||
|
||||
struct Point {
|
||||
@ -43,6 +45,12 @@ impl State {
|
||||
// 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::ChangeColor(t) => self.change_color(t),
|
||||
Message::Echo(s) => self.echo(s),
|
||||
Message::Move(Point{x,y}) => self.move_position(Point{x, y}),
|
||||
Message::Quit => self.quit()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user