From b63f6a6a9ef3f51a8de888cf98cbf5db5725ca1b Mon Sep 17 00:00:00 2001 From: Suzie Kim Date: Wed, 31 Jan 2024 17:16:45 -0500 Subject: [PATCH] Complete strings exercises --- exercises/09_strings/strings1.rs | 4 ++-- exercises/09_strings/strings2.rs | 5 +++-- exercises/09_strings/strings3.rs | 18 ++++++++++++++---- exercises/09_strings/strings4.rs | 22 ++++++++++------------ 4 files changed, 29 insertions(+), 20 deletions(-) diff --git a/exercises/09_strings/strings1.rs b/exercises/09_strings/strings1.rs index f50e1fa9..b64c651c 100644 --- a/exercises/09_strings/strings1.rs +++ b/exercises/09_strings/strings1.rs @@ -5,7 +5,6 @@ // Execute `rustlings hint strings1` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE fn main() { let answer = current_favorite_color(); @@ -13,5 +12,6 @@ fn main() { } fn current_favorite_color() -> String { - "blue" + // "blue".to_string() // can be used to make strings from different types (aka integers, floats, chars, etc.) + String::from("blue") // can only be used to make strings from &string(string slices) } diff --git a/exercises/09_strings/strings2.rs b/exercises/09_strings/strings2.rs index 4d95d16a..8ab11b91 100644 --- a/exercises/09_strings/strings2.rs +++ b/exercises/09_strings/strings2.rs @@ -5,11 +5,12 @@ // 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) { + + // This needs to be a string slice because the function is expecting a string slice. + 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/09_strings/strings3.rs b/exercises/09_strings/strings3.rs index b29f9325..13dbe919 100644 --- a/exercises/09_strings/strings3.rs +++ b/exercises/09_strings/strings3.rs @@ -3,21 +3,31 @@ // 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_string() } fn compose_me(input: &str) -> String { // TODO: Add " world!" to the string! There's multiple ways to do this! - ??? + + // let mut new_string = String::new(); + // new_string.push_str(input); + // new_string.push_str(" world!"); + // new_string + + format!("{input} {}", "world!") + + // let a = input.to_string(); + // let b = " world!".to_string(); + // a + &b + } fn replace_me(input: &str) -> String { // TODO: Replace "cars" in the string with "balloons"! - ??? + input.replace("cars", "balloons") } #[cfg(test)] diff --git a/exercises/09_strings/strings4.rs b/exercises/09_strings/strings4.rs index e8c54acc..deda20d4 100644 --- a/exercises/09_strings/strings4.rs +++ b/exercises/09_strings/strings4.rs @@ -7,8 +7,6 @@ // // No hints this time! -// I AM NOT DONE - fn string_slice(arg: &str) { println!("{}", arg); } @@ -17,14 +15,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()); // this is cloning + string_slice("nice weather".into()); // calls it self + 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()); }