mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-07 03:09:19 +00:00
finish enums exercises
This commit is contained in:
parent
3d6fb762ae
commit
b26aaf2095
@ -1,11 +1,12 @@
|
|||||||
// enums1.rs
|
// enums1.rs
|
||||||
// Make me compile! Execute `rustlings hint enums1` for hints!
|
// Make me compile! Execute `rustlings hint enums1` for hints!
|
||||||
|
|
||||||
// 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() {
|
||||||
|
|||||||
@ -1,11 +1,12 @@
|
|||||||
// enums2.rs
|
// enums2.rs
|
||||||
// Make me compile! Execute `rustlings hint enums2` for hints!
|
// Make me compile! Execute `rustlings hint enums2` for hints!
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
enum Message {
|
enum Message {
|
||||||
// TODO: define the different variants used below
|
Move { x: u8, y: u8 },
|
||||||
|
Echo(String),
|
||||||
|
ChangeColor(u8, u8, u8),
|
||||||
|
Quit,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Message {
|
impl Message {
|
||||||
@ -16,10 +17,10 @@ impl Message {
|
|||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let messages = [
|
let messages = [
|
||||||
Message::Move{ x: 10, y: 30 },
|
Message::Move { x: 10, y: 30 },
|
||||||
Message::Echo(String::from("hello world")),
|
Message::Echo(String::from("hello world")),
|
||||||
Message::ChangeColor(200, 255, 255),
|
Message::ChangeColor(200, 255, 255),
|
||||||
Message::Quit
|
Message::Quit,
|
||||||
];
|
];
|
||||||
|
|
||||||
for message in &messages {
|
for message in &messages {
|
||||||
|
|||||||
@ -1,21 +1,22 @@
|
|||||||
// enums3.rs
|
// enums3.rs
|
||||||
// Address all the TODOs to make the tests pass!
|
// Address all the TODOs to make the tests pass!
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
enum Message {
|
enum Message {
|
||||||
// TODO: implement the message variant types based on their usage below
|
Move(Point),
|
||||||
|
Echo(String),
|
||||||
|
ChangeColor(u8, u8, u8),
|
||||||
|
Quit,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Point {
|
struct Point {
|
||||||
x: u8,
|
x: u8,
|
||||||
y: u8
|
y: u8,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct State {
|
struct State {
|
||||||
color: (u8, u8, u8),
|
color: (u8, u8, u8),
|
||||||
position: Point,
|
position: Point,
|
||||||
quit: bool
|
quit: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl State {
|
impl State {
|
||||||
@ -36,7 +37,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 {
|
||||||
|
Message::ChangeColor(r, g, b) => self.change_color((r, g, b)),
|
||||||
|
Message::Quit => self.quit(),
|
||||||
|
Message::Echo(s) => self.echo(s),
|
||||||
|
Message::Move(p) => self.move_position(p),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -46,14 +52,14 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_match_message_call() {
|
fn test_match_message_call() {
|
||||||
let mut state = State{
|
let mut state = State {
|
||||||
quit: false,
|
quit: false,
|
||||||
position: Point{ x: 0, y: 0 },
|
position: Point { x: 0, y: 0 },
|
||||||
color: (0, 0, 0)
|
color: (0, 0, 0),
|
||||||
};
|
};
|
||||||
state.process(Message::ChangeColor(255, 0, 255));
|
state.process(Message::ChangeColor(255, 0, 255));
|
||||||
state.process(Message::Echo(String::from("hello world")));
|
state.process(Message::Echo(String::from("hello world")));
|
||||||
state.process(Message::Move(Point{ x: 10, y: 15 }));
|
state.process(Message::Move(Point { x: 10, y: 15 }));
|
||||||
state.process(Message::Quit);
|
state.process(Message::Quit);
|
||||||
|
|
||||||
assert_eq!(state.color, (255, 0, 255));
|
assert_eq!(state.color, (255, 0, 255));
|
||||||
@ -61,5 +67,4 @@ mod tests {
|
|||||||
assert_eq!(state.position.y, 15);
|
assert_eq!(state.position.y, 15);
|
||||||
assert_eq!(state.quit, true);
|
assert_eq!(state.quit, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user