From cf534fa0b80c26c6bbf141a85e89a2fa7e9bf9c1 Mon Sep 17 00:00:00 2001 From: Robert Zhao Date: Fri, 9 Jun 2023 18:30:16 -0400 Subject: [PATCH] Complete strings exercises --- exercises/strings/strings1.rs | 4 +--- exercises/strings/strings2.rs | 6 ++---- exercises/strings/strings3.rs | 21 +++++++++++---------- exercises/strings/strings4.rs | 22 ++++++++++------------ 4 files changed, 24 insertions(+), 29 deletions(-) diff --git a/exercises/strings/strings1.rs b/exercises/strings/strings1.rs index 0de86a1d..1211c9ea 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" + String::from("blue") } diff --git a/exercises/strings/strings2.rs b/exercises/strings/strings2.rs index 0c48ec95..2b9486ca 100644 --- a/exercises/strings/strings2.rs +++ b/exercises/strings/strings2.rs @@ -2,17 +2,15 @@ // 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."); } } -fn is_a_color_word(attempt: &str) -> bool { +fn is_a_color_word(attempt: &String) -> bool { attempt == "green" || attempt == "blue" || attempt == "red" } diff --git a/exercises/strings/strings3.rs b/exercises/strings/strings3.rs index e2353aec..75b7fb11 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! - ??? + String::from(input.trim()) } 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"! - ??? + String::from(input.replace("cars", "balloons")) } #[cfg(test)] @@ -37,7 +32,13 @@ mod tests { #[test] fn replace_a_string() { - assert_eq!(replace_me("I think cars are cool"), "I think balloons are cool"); - assert_eq!(replace_me("I love to look at cars"), "I love to look at balloons"); + assert_eq!( + replace_me("I think cars are cool"), + "I think balloons are cool" + ); + assert_eq!( + replace_me("I love to look at cars"), + "I love to look at balloons" + ); } } 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()); }