From 64d95837e9813541cf5b357de13865ce687ae98d Mon Sep 17 00:00:00 2001 From: Adam Brewer Date: Mon, 16 Oct 2023 07:37:12 -0400 Subject: Update Exercises Directory Names to Reflect Order --- exercises/09_strings/README.md | 9 ++++++++ exercises/09_strings/strings1.rs | 17 +++++++++++++++ exercises/09_strings/strings2.rs | 21 +++++++++++++++++++ exercises/09_strings/strings3.rs | 45 ++++++++++++++++++++++++++++++++++++++++ exercises/09_strings/strings4.rs | 30 +++++++++++++++++++++++++++ 5 files changed, 122 insertions(+) create mode 100644 exercises/09_strings/README.md create mode 100644 exercises/09_strings/strings1.rs create mode 100644 exercises/09_strings/strings2.rs create mode 100644 exercises/09_strings/strings3.rs create mode 100644 exercises/09_strings/strings4.rs (limited to 'exercises/09_strings') diff --git a/exercises/09_strings/README.md b/exercises/09_strings/README.md new file mode 100644 index 0000000..fa2104c --- /dev/null +++ b/exercises/09_strings/README.md @@ -0,0 +1,9 @@ +# Strings + +Rust has two string types, a string slice (`&str`) and an owned string (`String`). +We're not going to dictate when you should use which one, but we'll show you how +to identify and create them, as well as use them. + +## Further information + +- [Strings](https://doc.rust-lang.org/book/ch08-02-strings.html) diff --git a/exercises/09_strings/strings1.rs b/exercises/09_strings/strings1.rs new file mode 100644 index 0000000..f50e1fa --- /dev/null +++ b/exercises/09_strings/strings1.rs @@ -0,0 +1,17 @@ +// strings1.rs +// +// 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" +} diff --git a/exercises/09_strings/strings2.rs b/exercises/09_strings/strings2.rs new file mode 100644 index 0000000..4d95d16 --- /dev/null +++ b/exercises/09_strings/strings2.rs @@ -0,0 +1,21 @@ +// strings2.rs +// +// 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) { + 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 { + attempt == "green" || attempt == "blue" || attempt == "red" +} diff --git a/exercises/09_strings/strings3.rs b/exercises/09_strings/strings3.rs new file mode 100644 index 0000000..b29f932 --- /dev/null +++ b/exercises/09_strings/strings3.rs @@ -0,0 +1,45 @@ +// 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! + ??? +} + +fn compose_me(input: &str) -> String { + // TODO: Add " world!" to the string! There's multiple ways to do this! + ??? +} + +fn replace_me(input: &str) -> String { + // TODO: Replace "cars" in the string with "balloons"! + ??? +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn trim_a_string() { + assert_eq!(trim_me("Hello! "), "Hello!"); + assert_eq!(trim_me(" What's up!"), "What's up!"); + assert_eq!(trim_me(" Hola! "), "Hola!"); + } + + #[test] + fn compose_a_string() { + assert_eq!(compose_me("Hello"), "Hello world!"); + assert_eq!(compose_me("Goodbye"), "Goodbye world!"); + } + + #[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"); + } +} diff --git a/exercises/09_strings/strings4.rs b/exercises/09_strings/strings4.rs new file mode 100644 index 0000000..e8c54ac --- /dev/null +++ b/exercises/09_strings/strings4.rs @@ -0,0 +1,30 @@ +// strings4.rs +// +// Ok, here are a bunch of values-- some are `String`s, some are `&str`s. Your +// task is to call one of these two functions on each value depending on what +// you think each value is. That is, add either `string_slice` or `string` +// 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); +} +fn string(arg: String) { + println!("{}", arg); +} + +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()); +} -- cgit v1.2.3