Skip to content

Commit

Permalink
Enum exercises
Browse files Browse the repository at this point in the history
  • Loading branch information
Joaopmorais committed Feb 19, 2024
1 parent 28bfb1c commit 2dd27d5
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
2 changes: 1 addition & 1 deletion exercises/07_structs/structs3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
struct Package {
sender_country: String,
recipient_country: String,
weight_in_grams: u32,
weight_in_grams: u32,
}

impl Package {
Expand Down
7 changes: 5 additions & 2 deletions exercises/08_enums/enums1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
//
// 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/08_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
Quit,
Echo (String) ,
Move {x: u32,y:u32},
ChangeColor (i32, i32, i32),
}

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

struct Point {
Expand Down Expand Up @@ -44,6 +46,13 @@ impl State {
// 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(a,b,c) => self.change_color((a,b,c)),
Message:: Echo (m) => self.echo(m),
Message:: Quit => self.quit(),
Message:: Move(x) => self.move_position(x),
}

}
}

Expand Down

0 comments on commit 2dd27d5

Please sign in to comment.