enums done

This commit is contained in:
Himan-Miku 2023-04-19 19:46:04 +05:30
parent c8fb4bd350
commit 185220d2bc
5 changed files with 30 additions and 10 deletions

View File

@ -1,16 +1,18 @@
// 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(String),
Move { x: i32, y: i32 },
ChangeColor(i32, i32, i32),
}
fn main() {
println!("{:?}", Message::Quit);
println!("{:?}", Message::Echo);
println!("{:?}", Message::Move);
println!("{:?}", Message::ChangeColor);
println!("{:?}", Message::Echo(String::from("Hello")));
println!("{:?}", Message::Move { x: 4, y: 5 });
println!("{:?}", Message::ChangeColor(0, 255, 78));
}

View File

@ -1,11 +1,13 @@
// 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 {

View File

@ -2,10 +2,12 @@
// 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
Quit,
ChangeColor((u8, u8, u8)),
Echo(String),
Move(Point),
}
struct Point {
@ -39,6 +41,20 @@ impl State {
fn process(&mut self, message: Message) {
// TODO: create a match expression to process the different message 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::Quit => {
self.quit();
}
Message::ChangeColor(color) => {
self.change_color(color);
}
Message::Echo(some_string) => {
self.echo(some_string);
}
Message::Move(point) => {
self.move_position(point);
}
}
}
}
@ -53,7 +69,7 @@ mod tests {
position: Point { x: 0, y: 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::Move(Point { x: 10, y: 15 }));
state.process(Message::Quit);

BIN
temp_13468_ThreadId1.exe Normal file

Binary file not shown.

BIN
temp_9728_ThreadId1.exe Normal file

Binary file not shown.