[Add] exercises/enums

This commit is contained in:
Phan Huy Hoang 2023-01-03 16:43:23 +07:00
parent 21ec90c3c7
commit e9569b0a49
3 changed files with 18 additions and 11 deletions

View File

@ -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 Quit,
Echo,
Move,
ChangeColor
} }
fn main() { fn main() {

View File

@ -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 Quit,
Echo(String),
ChangeColor(isize, isize, isize),
Move { x: isize, y: isize },
} }
impl Message { impl Message {

View File

@ -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 Quit,
Move(Point),
Echo(String),
ChangeColor(u8, u8, u8),
} }
struct Point { struct Point {
@ -37,8 +38,12 @@ 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 match message {
// Remember: When passing a tuple as a function argument, you'll need extra parentheses: fn function((t, u, p, l, e)) Message::Quit => self.quit = true,
Message::Move(point) => self.position = point,
Message::Echo(_) => {},
Message::ChangeColor(r, g, b) => self.color = (r, g, b),
}
} }
} }