Last active
December 1, 2023 22:21
-
-
Save m0veax/805b10b5aefe5f698b7d7d550be679b8 to your computer and use it in GitHub Desktop.
rustlings problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// enums3.rs | |
// | |
// 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), | |
Echo(String), | |
Move(Point), | |
Quit | |
} | |
struct Point { | |
x: u8, | |
y: u8, | |
} | |
struct State { | |
color: (u8, u8, u8), | |
position: Point, | |
quit: bool, | |
message: String, | |
} | |
impl State { | |
fn change_color(&mut self, color: (u8, u8, u8)) { | |
self.color = color; | |
} | |
fn quit(&mut self) { | |
self.quit = true; | |
} | |
fn echo(&mut self, s: String) { | |
self.message = s | |
} | |
fn move_position(&mut self, p: Point) { | |
self.position = p; | |
} | |
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 => self.change_color((message.ChangeColor.0, message.ChangeColor.1, message.ChangeColor.2)), | |
Message::Echo => self.echo(Message::Echo), | |
Message::Move => self.move_position(Message::Move), | |
Message::Quit => self.quit() | |
}; | |
} | |
} | |
#[cfg(test)] | |
mod tests { | |
use super::*; | |
#[test] | |
fn test_match_message_call() { | |
let mut state = State { | |
quit: false, | |
position: Point { x: 0, y: 0 }, | |
color: (0, 0, 0), | |
message: "hello world".to_string(), | |
}; | |
state.process(Message::ChangeColor(255, 0, 255)); | |
state.process(Message::Echo(String::from("Hello world!"))); | |
state.process(Message::Move(Point { x: 10, y: 15 })); | |
state.process(Message::Quit); | |
assert_eq!(state.color, (255, 0, 255)); | |
assert_eq!(state.position.x, 10); | |
assert_eq!(state.position.y, 15); | |
assert_eq!(state.quit, true); | |
assert_eq!(state.message, "Hello world!"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// enums3.rs | |
// | |
// 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), | |
Echo(String), | |
Move(Point), | |
Quit | |
} | |
struct Point { | |
x: u8, | |
y: u8, | |
} | |
struct State { | |
color: (u8, u8, u8), | |
position: Point, | |
quit: bool, | |
message: String, | |
} | |
impl State { | |
fn change_color(&mut self, color: (u8, u8, u8)) { | |
self.color = color; | |
} | |
fn quit(&mut self) { | |
self.quit = true; | |
} | |
fn echo(&mut self, s: String) { | |
self.message = s | |
} | |
fn move_position(&mut self, p: Point) { | |
self.position = p; | |
} | |
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::Echo(msg) => self.echo(msg), | |
Message::Move(p) => self.move_position(p), | |
Message::Quit => self.quit() | |
}; | |
} | |
} | |
#[cfg(test)] | |
mod tests { | |
use super::*; | |
#[test] | |
fn test_match_message_call() { | |
let mut state = State { | |
quit: false, | |
position: Point { x: 0, y: 0 }, | |
color: (0, 0, 0), | |
message: "hello world".to_string(), | |
}; | |
state.process(Message::ChangeColor(255, 0, 255)); | |
state.process(Message::Echo(String::from("Hello world!"))); | |
state.process(Message::Move(Point { x: 10, y: 15 })); | |
state.process(Message::Quit); | |
assert_eq!(state.color, (255, 0, 255)); | |
assert_eq!(state.position.x, 10); | |
assert_eq!(state.position.y, 15); | |
assert_eq!(state.quit, true); | |
assert_eq!(state.message, "Hello world!"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
⚠️ Compiling of exercises/08_enums/enums3.rs failed! Please try again. Here's the output: | |
error[E0532]: expected unit struct, unit variant or constant, found tuple variant `Message::ChangeColor` | |
--> exercises/08_enums/enums3.rs:52:13 | |
| | |
12 | ChangeColor(u8, u8, u8), | |
| ----------------------- `Message::ChangeColor` defined here | |
... | |
52 | Message::ChangeColor => self.change_color((message.ChangeColor.0, message.ChangeColor.1, message.ChangeColor.2)), | |
| ^^^^^^^^^^^^^^^^^^^^ help: use the tuple variant pattern syntax instead: `Message::ChangeColor(_, _, _)` | |
error[E0532]: expected unit struct, unit variant or constant, found tuple variant `Message::Echo` | |
--> exercises/08_enums/enums3.rs:53:13 | |
| | |
13 | Echo(String), | |
| ------------ `Message::Echo` defined here | |
... | |
53 | Message::Echo => self.echo(Message::Echo), | |
| ^^^^^^^^^^^^^ help: use the tuple variant pattern syntax instead: `Message::Echo(_)` | |
error[E0532]: expected unit struct, unit variant or constant, found tuple variant `Message::Move` | |
--> exercises/08_enums/enums3.rs:54:13 | |
| | |
14 | Move(Point), | |
| ----------- `Message::Move` defined here | |
... | |
54 | Message::Move => self.move_position(Message::Move), | |
| ^^^^^^^^^^^^^ help: use the tuple variant pattern syntax instead: `Message::Move(_)` | |
error[E0609]: no field `ChangeColor` on type `Message` | |
--> exercises/08_enums/enums3.rs:52:64 | |
| | |
52 | Message::ChangeColor => self.change_color((message.ChangeColor.0, message.ChangeColor.1, message.ChangeColor.2)), | |
| ^^^^^^^^^^^ | |
error[E0609]: no field `ChangeColor` on type `Message` | |
--> exercises/08_enums/enums3.rs:52:87 | |
| | |
52 | Message::ChangeColor => self.change_color((message.ChangeColor.0, message.ChangeColor.1, message.ChangeColor.2)), | |
| ^^^^^^^^^^^ | |
error[E0609]: no field `ChangeColor` on type `Message` | |
--> exercises/08_enums/enums3.rs:52:110 | |
| | |
52 | Message::ChangeColor => self.change_color((message.ChangeColor.0, message.ChangeColor.1, message.ChangeColor.2)), | |
| ^^^^^^^^^^^ | |
error[E0308]: mismatched types | |
--> exercises/08_enums/enums3.rs:53:40 | |
| | |
53 | Message::Echo => self.echo(Message::Echo), | |
| ---- ^^^^^^^^^^^^^ expected `String`, found enum constructor | |
| | | |
| arguments to this method are incorrect | |
| | |
= note: expected struct `String` | |
found enum constructor `fn(String) -> Message {Message::Echo}` | |
note: method defined here | |
--> exercises/08_enums/enums3.rs:39:8 | |
| | |
39 | fn echo(&mut self, s: String) { | |
| ^^^^ --------- | |
error[E0308]: mismatched types | |
--> exercises/08_enums/enums3.rs:54:49 | |
| | |
54 | Message::Move => self.move_position(Message::Move), | |
| ------------- ^^^^^^^^^^^^^ expected `Point`, found enum constructor | |
| | | |
| arguments to this method are incorrect | |
| | |
= note: expected struct `Point` | |
found enum constructor `fn(Point) -> Message {Message::Move}` | |
note: method defined here | |
--> exercises/08_enums/enums3.rs:43:8 | |
| | |
43 | fn move_position(&mut self, p: Point) { | |
| ^^^^^^^^^^^^^ -------- | |
error: aborting due to 8 previous errors | |
Some errors have detailed explanations: E0308, E0532, E0609. | |
For more information about an error, try `rustc --explain E0308`. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment