summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--exercises/09_strings/strings4.rs44
-rw-r--r--rustlings-macros/info.toml10
-rw-r--r--solutions/09_strings/strings4.rs39
3 files changed, 75 insertions, 18 deletions
diff --git a/exercises/09_strings/strings4.rs b/exercises/09_strings/strings4.rs
index 1f3d88b..9d9eb48 100644
--- a/exercises/09_strings/strings4.rs
+++ b/exercises/09_strings/strings4.rs
@@ -1,24 +1,36 @@
-// 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!
+// Calls of this function should be replaced with calls of `string_slice` or `string`.
+fn placeholder() {}
fn string_slice(arg: &str) {
- println!("{}", arg);
+ println!("{arg}");
}
fn string(arg: String) {
- println!("{}", arg);
+ println!("{arg}");
}
+// TODO: Here are a bunch of values - some are `String`, some are `&str`.
+// Your task is to replace `placeholder(…)` with either `string_slice(…)`
+// or `string(…)` depending on what you think each value is.
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());
+ placeholder("blue");
+
+ placeholder("red".to_string());
+
+ placeholder(String::from("hi"));
+
+ placeholder("rust is fun!".to_owned());
+
+ placeholder("nice weather".into());
+
+ placeholder(format!("Interpolation {}", "Station"));
+
+ // WARNING: This is byte indexing, not character indexing.
+ // Character indexing can be done using `s.chars().nth(INDEX)`.
+ placeholder(&String::from("abc")[0..1]);
+
+ placeholder(" hello there ".trim());
+
+ placeholder("Happy Monday!".replace("Mon", "Tues"));
+
+ placeholder("mY sHiFt KeY iS sTiCkY".to_lowercase());
}
diff --git a/rustlings-macros/info.toml b/rustlings-macros/info.toml
index 618fc91..7607650 100644
--- a/rustlings-macros/info.toml
+++ b/rustlings-macros/info.toml
@@ -510,7 +510,15 @@ the string slice into an owned string, which you can then freely extend."""
name = "strings4"
dir = "09_strings"
test = false
-hint = "No hints this time ;)"
+hint = """
+Replace `placeholder` with either `string` or `string_slice` in the `main` function.
+
+Example:
+`placeholder("blue");`
+should become
+`string_slice("blue");`
+because "blue" is `&str`, not `String`.
+"""
# MODULES
diff --git a/solutions/09_strings/strings4.rs b/solutions/09_strings/strings4.rs
index 4e18198..9dc6917 100644
--- a/solutions/09_strings/strings4.rs
+++ b/solutions/09_strings/strings4.rs
@@ -1 +1,38 @@
-// Solutions will be available before the stable release. Thank you for testing the beta version 🥰
+fn string_slice(arg: &str) {
+ println!("{arg}");
+}
+fn string(arg: String) {
+ println!("{arg}");
+}
+
+fn main() {
+ string_slice("blue");
+
+ string("red".to_string());
+
+ string(String::from("hi"));
+
+ string("rust is fun!".to_owned());
+
+ // Here, both answers work.
+ // `.into()` converts a type into an expected type.
+ // If it is called where `String` is expected, it will convert `&str` to `String`.
+ // But if is called where `&str` is expected, then `&str` is kept `&str` since no
+ // conversion is needed.
+ string("nice weather".into());
+ string_slice("nice weather".into());
+ // ^^^^^^^ the compiler recommends removing the `.into()`
+ // call because it is a useless conversion.
+
+ string(format!("Interpolation {}", "Station"));
+
+ // WARNING: This is byte indexing, not character indexing.
+ // Character indexing can be done using `s.chars().nth(INDEX)`.
+ string_slice(&String::from("abc")[0..1]);
+
+ string_slice(" hello there ".trim());
+
+ string("Happy Monday!".replace("Mon", "Tues"));
+
+ string("mY sHiFt KeY iS sTiCkY".to_lowercase());
+}