From e9569b0a49d85e06f3b1b4a51c0c67ba25e78bf7 Mon Sep 17 00:00:00 2001 From: Phan Huy Hoang Date: Tue, 3 Jan 2023 16:43:23 +0700 Subject: [PATCH 1/2] [Add] exercises/enums --- exercises/enums/enums1.rs | 7 ++++--- exercises/enums/enums2.rs | 7 ++++--- exercises/enums/enums3.rs | 15 ++++++++++----- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/exercises/enums/enums1.rs b/exercises/enums/enums1.rs index 511ba740..bdfb3d51 100644 --- a/exercises/enums/enums1.rs +++ b/exercises/enums/enums1.rs @@ -1,11 +1,12 @@ // enums1.rs // 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() { diff --git a/exercises/enums/enums2.rs b/exercises/enums/enums2.rs index 167a6b2e..77df8943 100644 --- a/exercises/enums/enums2.rs +++ b/exercises/enums/enums2.rs @@ -1,11 +1,12 @@ // enums2.rs // 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), + ChangeColor(isize, isize, isize), + Move { x: isize, y: isize }, } impl Message { diff --git a/exercises/enums/enums3.rs b/exercises/enums/enums3.rs index a2a9d586..f935eba4 100644 --- a/exercises/enums/enums3.rs +++ b/exercises/enums/enums3.rs @@ -2,10 +2,11 @@ // 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 + Quit, + Move(Point), + Echo(String), + ChangeColor(u8, u8, u8), } struct Point { @@ -37,8 +38,12 @@ impl State { } 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::Quit => self.quit = true, + Message::Move(point) => self.position = point, + Message::Echo(_) => {}, + Message::ChangeColor(r, g, b) => self.color = (r, g, b), + } } } From 1f9e8e4c4489a596ce001a966cde451f7031f635 Mon Sep 17 00:00:00 2001 From: Phan Huy Hoang Date: Tue, 3 Jan 2023 16:46:40 +0700 Subject: [PATCH 2/2] [Add] exercises/strings --- exercises/strings/strings1.rs | 4 +--- exercises/strings/strings2.rs | 4 +--- exercises/strings/strings3.rs | 11 +++-------- exercises/strings/strings4.rs | 22 ++++++++++------------ 4 files changed, 15 insertions(+), 26 deletions(-) diff --git a/exercises/strings/strings1.rs b/exercises/strings/strings1.rs index 0de86a1d..fd74ea38 100644 --- a/exercises/strings/strings1.rs +++ b/exercises/strings/strings1.rs @@ -2,13 +2,11 @@ // Make me compile without changing the function signature! // Execute `rustlings hint strings1` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - fn main() { let answer = current_favorite_color(); println!("My current favorite color is {}", answer); } fn current_favorite_color() -> String { - "blue" + "blue".to_string() } diff --git a/exercises/strings/strings2.rs b/exercises/strings/strings2.rs index 0c48ec95..22e8986e 100644 --- a/exercises/strings/strings2.rs +++ b/exercises/strings/strings2.rs @@ -2,11 +2,9 @@ // Make me compile without changing the function signature! // Execute `rustlings hint strings2` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - fn main() { let word = String::from("green"); // Try not changing this line :) - if is_a_color_word(word) { + if is_a_color_word(&word) { println!("That is a color word I know!"); } else { println!("That is not a color word I know."); diff --git a/exercises/strings/strings3.rs b/exercises/strings/strings3.rs index e2353aec..f6d7df58 100644 --- a/exercises/strings/strings3.rs +++ b/exercises/strings/strings3.rs @@ -1,21 +1,16 @@ // strings3.rs // Execute `rustlings hint strings3` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - fn trim_me(input: &str) -> String { - // TODO: Remove whitespace from both ends of a string! - ??? + input.trim().to_owned() } fn compose_me(input: &str) -> String { - // TODO: Add " world!" to the string! There's multiple ways to do this! - ??? + format!("{input} world!") } fn replace_me(input: &str) -> String { - // TODO: Replace "cars" in the string with "balloons"! - ??? + input.replace("cars", "balloons") } #[cfg(test)] diff --git a/exercises/strings/strings4.rs b/exercises/strings/strings4.rs index c410b562..0205f9c3 100644 --- a/exercises/strings/strings4.rs +++ b/exercises/strings/strings4.rs @@ -6,8 +6,6 @@ // before the parentheses on each line. If you're right, it will compile! // No hints this time! -// I AM NOT DONE - fn string_slice(arg: &str) { println!("{}", arg); } @@ -16,14 +14,14 @@ fn string(arg: String) { } fn main() { - ???("blue"); - ???("red".to_string()); - ???(String::from("hi")); - ???("rust is fun!".to_owned()); - ???("nice weather".into()); - ???(format!("Interpolation {}", "Station")); - ???(&String::from("abc")[0..1]); - ???(" hello there ".trim()); - ???("Happy Monday!".to_string().replace("Mon", "Tues")); - ???("mY sHiFt KeY iS sTiCkY".to_lowercase()); + string_slice("blue"); + string("red".to_string()); + string(String::from("hi")); + string("rust is fun!".to_owned()); + string("nice weather".into()); + string(format!("Interpolation {}", "Station")); + string_slice(&String::from("abc")[0..1]); + string_slice(" hello there ".trim()); + string("Happy Monday!".to_string().replace("Mon", "Tues")); + string("mY sHiFt KeY iS sTiCkY".to_lowercase()); }