summaryrefslogtreecommitdiff
path: root/exercises/08_enums
diff options
context:
space:
mode:
Diffstat (limited to 'exercises/08_enums')
-rw-r--r--exercises/08_enums/enums1.rs8
-rw-r--r--exercises/08_enums/enums2.rs12
-rw-r--r--exercises/08_enums/enums3.rs36
3 files changed, 20 insertions, 36 deletions
diff --git a/exercises/08_enums/enums1.rs b/exercises/08_enums/enums1.rs
index ab87270..c65e641 100644
--- a/exercises/08_enums/enums1.rs
+++ b/exercises/08_enums/enums1.rs
@@ -1,12 +1,6 @@
-// enums1.rs
-//
-// No hints this time! ;)
-
-// I AM NOT DONE
-
#[derive(Debug)]
enum Message {
- // TODO: define a few types of messages as used below
+ // TODO: Define a few types of messages as used below.
}
fn main() {
diff --git a/exercises/08_enums/enums2.rs b/exercises/08_enums/enums2.rs
index 133c779..5a74991 100644
--- a/exercises/08_enums/enums2.rs
+++ b/exercises/08_enums/enums2.rs
@@ -1,13 +1,7 @@
-// enums2.rs
-//
-// Execute `rustlings hint enums2` or use the `hint` watch subcommand for a
-// hint.
-
-// I AM NOT DONE
-
+#[allow(dead_code)]
#[derive(Debug)]
enum Message {
- // TODO: define the different variants used below
+ // TODO: Define the different variants used below.
}
#[derive(Debug)]
@@ -18,7 +12,7 @@ struct Point {
impl Message {
fn call(&self) {
- println!("{:?}", self);
+ println!("{self:?}");
}
}
diff --git a/exercises/08_enums/enums3.rs b/exercises/08_enums/enums3.rs
index fffb9d0..2d0d82d 100644
--- a/exercises/08_enums/enums3.rs
+++ b/exercises/08_enums/enums3.rs
@@ -1,14 +1,5 @@
-// 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
+ // TODO: Implement the message variant types based on their usage below.
}
struct Point {
@@ -35,25 +26,29 @@ impl State {
}
fn echo(&mut self, s: String) {
- self.message = s
+ self.message = s;
}
- fn resize(&mut self, w: u8, h: u8) {
- self.width = w;
- self.height = h;
+ fn resize(&mut self, width: u8, height: u8) {
+ self.width = width;
+ self.height = height;
}
- fn move_position(&mut self, p: Point) {
- self.position = p;
+ fn move_position(&mut self, point: Point) {
+ self.position = point;
}
fn process(&mut self, message: Message) {
- // TODO: create a match expression to process the different message variants
+ // 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))
+ // e.g. `foo((t, u, p, l, e))`
}
}
+fn main() {
+ // You can optionally experiment here.
+}
+
#[cfg(test)]
mod tests {
use super::*;
@@ -66,8 +61,9 @@ mod tests {
height: 0,
position: Point { x: 0, y: 0 },
color: (0, 0, 0),
- message: "hello world".to_string(),
+ 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 });
@@ -79,7 +75,7 @@ mod tests {
assert_eq!(state.height, 30);
assert_eq!(state.position.x, 10);
assert_eq!(state.position.y, 15);
- assert_eq!(state.quit, true);
+ assert!(state.quit);
assert_eq!(state.message, "Hello world!");
}
}