Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make enum variants more consistent between exercises #1774

Merged
merged 3 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions exercises/08_enums/enums1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ enum Message {
fn main() {
println!("{:?}", Message::Quit);
println!("{:?}", Message::Echo);
println!("{:?}", Message::Resize);
println!("{:?}", Message::Move);
println!("{:?}", Message::ChangeColor);
}
9 changes: 8 additions & 1 deletion exercises/08_enums/enums2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ enum Message {
// TODO: Define the different variants used below.
}

#[derive(Debug)]
struct Point {
x: u8,
y: u8,
}

impl Message {
fn call(&self) {
println!("{self:?}");
Expand All @@ -12,7 +18,8 @@ impl Message {

fn main() {
let messages = [
Message::Move { x: 10, y: 30 },
Message::Resize { w: 10, h: 30 },
Message::Move(Point { x: 10, y: 15 }),
Message::Echo(String::from("hello world")),
Message::ChangeColor(200, 255, 255),
Message::Quit,
Expand Down
12 changes: 12 additions & 0 deletions exercises/08_enums/enums3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ struct Point {

struct State {
color: (u8, u8, u8),
width: u8,
height: u8,
position: Point,
quit: bool,
message: String,
Expand All @@ -27,6 +29,11 @@ impl State {
self.message = s;
}

fn resize(&mut self, width: u8, height: u8) {
self.width = width;
self.height = height;
}

fn move_position(&mut self, point: Point) {
self.position = point;
}
Expand All @@ -50,17 +57,22 @@ mod tests {
fn test_match_message_call() {
let mut state = State {
quit: false,
width: 0,
height: 0,
position: Point { x: 0, y: 0 },
color: (0, 0, 0),
message: String::from("hello world"),
};

state.process(Message::ChangeColor(255, 0, 255));
state.process(Message::Echo(String::from("Hello world!")));
state.process(Message::Resize { w: 10, h: 30 });
state.process(Message::Move(Point { x: 10, y: 15 }));
state.process(Message::Quit);

assert_eq!(state.color, (255, 0, 255));
assert_eq!(state.width, 10);
assert_eq!(state.height, 30);
assert_eq!(state.position.x, 10);
assert_eq!(state.position.y, 15);
assert!(state.quit);
Expand Down
2 changes: 1 addition & 1 deletion rustlings-macros/info.toml
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ dir = "08_enums"
test = false
hint = """
You can create enumerations that have different variants with different types
such as no data, anonymous structs, a single string, tuples, etc."""
such as anonymous structs, structs, a single string, tuples, no data, etc."""

[[exercises]]
name = "enums3"
Expand Down