mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-12 13:49:19 +00:00
enums
This commit is contained in:
parent
2f8e64d118
commit
0b3e8d79f9
@ -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,16 @@
|
||||
// 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
|
||||
Move {
|
||||
x: i32,
|
||||
y: i32,
|
||||
},
|
||||
Echo(String),
|
||||
ChangeColor(i32, i32, i32),
|
||||
Quit
|
||||
}
|
||||
|
||||
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
|
||||
Move(Point),
|
||||
Echo(String),
|
||||
ChangeColor((u8, u8, u8)),
|
||||
Quit
|
||||
}
|
||||
|
||||
struct Point {
|
||||
@ -37,7 +38,12 @@ impl State {
|
||||
}
|
||||
|
||||
fn process(&mut self, message: Message) {
|
||||
// TODO: create a match expression to process the different message variants
|
||||
match message {
|
||||
Message::Echo(string) => println!("{}", string),
|
||||
Message::Move(point) => self.position = point,
|
||||
Message::ChangeColor(color) => self.color = color,
|
||||
Message::Quit => self.quit = true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -33,7 +33,7 @@ mod my_module {
|
||||
Command::Uppercase => content.to_uppercase(),
|
||||
Command::Trim => content.trim().to_string(),
|
||||
Command::Append(n) => format!("{}{}", content, "bar".repeat(*n))
|
||||
}
|
||||
}
|
||||
}).collect()
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user