Complete enum exercises

This commit is contained in:
Suzie Kim 2024-01-31 16:54:23 -05:00
parent 4b14488a4b
commit 3c0941fc2c
No known key found for this signature in database
GPG key ID: 83C4CC3808F9AAE9
3 changed files with 19 additions and 6 deletions

View file

@ -2,13 +2,16 @@
// //
// 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() {
println!("{:?}", Message::Quit); println!("{:?}", Message::Quit);
println!("{:?}", Message::Echo); println!("{:?}", Message::Echo);

View file

@ -3,11 +3,13 @@
// Execute `rustlings hint enums2` or use the `hint` watch subcommand for a // Execute `rustlings hint enums2` or use the `hint` watch subcommand for a
// hint. // 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:i32, y:i32 },
Echo(String),
ChangeColor(i32, i32, i32),
Quit,
} }
impl Message { impl Message {

View file

@ -5,10 +5,12 @@
// Execute `rustlings hint enums3` or use the `hint` watch subcommand for a // Execute `rustlings hint enums3` or use the `hint` watch subcommand for a
// hint. // 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
Quit,
Echo(String),
Move(Point),
ChangeColor(u8, u8, u8),
} }
struct Point { struct Point {
@ -44,6 +46,12 @@ impl State {
// TODO: create a match expression to process the different message variants // 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: // Remember: When passing a tuple as a function argument, you'll need extra parentheses:
// fn function((t, u, p, l, e)) // fn function((t, u, p, l, e))
match message {
Message::ChangeColor(r, g, b) => self.change_color((r, g, b)),
Message::Echo(string) => self.echo(string),
Message::Move(point) => self.move_position(point),
Message::Quit => self.quit(),
}
} }
} }