Skip to content

Commit

Permalink
enums
Browse files Browse the repository at this point in the history
  • Loading branch information
afresquet committed Apr 19, 2023
1 parent 8506651 commit be07b03
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
7 changes: 4 additions & 3 deletions exercises/enums/enums1.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// 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,
Move,
ChangeColor,
}

fn main() {
Expand Down
7 changes: 4 additions & 3 deletions exercises/enums/enums2.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// 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(u8, u8, u8),
Quit,
}

impl Message {
Expand Down
13 changes: 10 additions & 3 deletions exercises/enums/enums3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
// 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
ChangeColor(u8, u8, u8),
Quit,
Echo(String),
Move(Point),
}

struct Point {
Expand Down Expand Up @@ -39,6 +40,12 @@ 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::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),
}
}
}

Expand Down

0 comments on commit be07b03

Please sign in to comment.