Skip to content

Commit

Permalink
finish enums problems.
Browse files Browse the repository at this point in the history
  • Loading branch information
a08001212 committed Jan 28, 2025
1 parent 8dd6d32 commit aaa9881
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
3 changes: 2 additions & 1 deletion exercises/enums/enums1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
//
// 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
5 changes: 4 additions & 1 deletion exercises/enums/enums2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
// 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 {
Expand Down
18 changes: 17 additions & 1 deletion exercises/enums/enums3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
// 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
Move(Point),
ChangeColor(u8, u8, u8),
Echo(String),
Quit,
}

struct Point {
Expand Down Expand Up @@ -43,6 +46,19 @@ impl State {
// 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::Move(p) => { self.move_position(p) },
Message::ChangeColor(r, g, b) => {
self.change_color((r, g, b));
},
Message::Echo(s) => {
self.echo(s);
},
Message::Quit => {
self.quit();
}
}
}
}

Expand Down

0 comments on commit aaa9881

Please sign in to comment.