mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-10 20:59:19 +00:00
✨ 🦀 Enums Solved
This commit is contained in:
parent
c67ee75a9d
commit
ee8a4c88dc
@ -1,11 +1,13 @@
|
|||||||
// 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
|
// TODO: define a few types of messages as used below
|
||||||
|
Quit,
|
||||||
|
Echo,
|
||||||
|
Move,
|
||||||
|
ChangeColor,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|||||||
@ -1,11 +1,13 @@
|
|||||||
// 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
|
// TODO: define the different variants used below
|
||||||
|
Move { x: i16, y: i16 },
|
||||||
|
Echo(String),
|
||||||
|
ChangeColor(i32, i32, i32),
|
||||||
|
Quit,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Message {
|
impl Message {
|
||||||
|
|||||||
@ -2,10 +2,12 @@
|
|||||||
// 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
|
// TODO: implement the message variant types based on their usage below
|
||||||
|
Move(Point),
|
||||||
|
Echo(String),
|
||||||
|
ChangeColor((u8, u8, u8)),
|
||||||
|
Quit,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Point {
|
struct Point {
|
||||||
@ -38,6 +40,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
|
// TODO: create a match expression to process the different message variants
|
||||||
|
match message {
|
||||||
|
Message::Move(p) => self.move_position(p),
|
||||||
|
Message::Echo(s) => self.echo(s),
|
||||||
|
Message::ChangeColor(color) => self.change_color(color),
|
||||||
|
Message::Quit => self.quit(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user