+6
-2
.rustlings-state.txt
+6
-2
.rustlings-state.txt
+1
-1
exercises/09_strings/strings1.rs
+1
-1
exercises/09_strings/strings1.rs
+1
-1
exercises/09_strings/strings2.rs
+1
-1
exercises/09_strings/strings2.rs
+3
exercises/09_strings/strings3.rs
+3
exercises/09_strings/strings3.rs
···
1
1
fn trim_me(input: &str) -> &str {
2
2
// TODO: Remove whitespace from both ends of a string.
3
+
input.trim()
3
4
}
4
5
5
6
fn compose_me(input: &str) -> String {
6
7
// TODO: Add " world!" to the string! There are multiple ways to do this.
8
+
format!("{} world!", input)
7
9
}
8
10
9
11
fn replace_me(input: &str) -> String {
10
12
// TODO: Replace "cars" in the string with "balloons".
13
+
input.replace("cars", "balloons")
11
14
}
12
15
13
16
fn main() {
+10
-10
exercises/09_strings/strings4.rs
+10
-10
exercises/09_strings/strings4.rs
···
13
13
// Your task is to replace `placeholder(…)` with either `string_slice(…)`
14
14
// or `string(…)` depending on what you think each value is.
15
15
fn main() {
16
-
placeholder("blue");
16
+
string_slice("blue");
17
17
18
-
placeholder("red".to_string());
18
+
string("red".to_string());
19
19
20
-
placeholder(String::from("hi"));
20
+
string(String::from("hi"));
21
21
22
-
placeholder("rust is fun!".to_owned());
22
+
string("rust is fun!".to_owned());
23
23
24
-
placeholder("nice weather".into());
24
+
string_slice("nice weather".into());
25
25
26
-
placeholder(format!("Interpolation {}", "Station"));
26
+
string(format!("Interpolation {}", "Station"));
27
27
28
28
// WARNING: This is byte indexing, not character indexing.
29
29
// Character indexing can be done using `s.chars().nth(INDEX)`.
30
-
placeholder(&String::from("abc")[0..1]);
30
+
string_slice(&String::from("abc")[0..1]);
31
31
32
-
placeholder(" hello there ".trim());
32
+
string_slice(" hello there ".trim());
33
33
34
-
placeholder("Happy Monday!".replace("Mon", "Tues"));
34
+
string("Happy Monday!".replace("Mon", "Tues"));
35
35
36
-
placeholder("mY sHiFt KeY iS sTiCkY".to_lowercase());
36
+
string("mY sHiFt KeY iS sTiCkY".to_lowercase());
37
37
}
+7
-2
solutions/09_strings/strings1.rs
+7
-2
solutions/09_strings/strings1.rs
···
1
+
fn current_favorite_color() -> String {
2
+
// Equivalent to `String::from("blue")`
3
+
"blue".to_string()
4
+
}
5
+
1
6
fn main() {
2
-
// DON'T EDIT THIS SOLUTION FILE!
3
-
// It will be automatically filled after you finish the exercise.
7
+
let answer = current_favorite_color();
8
+
println!("My current favorite color is {answer}");
4
9
}
+13
-2
solutions/09_strings/strings2.rs
+13
-2
solutions/09_strings/strings2.rs
···
1
+
fn is_a_color_word(attempt: &str) -> bool {
2
+
attempt == "green" || attempt == "blue" || attempt == "red"
3
+
}
4
+
1
5
fn main() {
2
-
// DON'T EDIT THIS SOLUTION FILE!
3
-
// It will be automatically filled after you finish the exercise.
6
+
let word = String::from("green");
7
+
8
+
if is_a_color_word(&word) {
9
+
// ^ added to have `&String` which is automatically
10
+
// coerced to `&str` by the compiler.
11
+
println!("That is a color word I know!");
12
+
} else {
13
+
println!("That is not a color word I know.");
14
+
}
4
15
}
+46
-2
solutions/09_strings/strings3.rs
+46
-2
solutions/09_strings/strings3.rs
···
1
+
fn trim_me(input: &str) -> &str {
2
+
input.trim()
3
+
}
4
+
5
+
fn compose_me(input: &str) -> String {
6
+
// The macro `format!` has the same syntax as `println!`, but it returns a
7
+
// string instead of printing it to the terminal.
8
+
// Equivalent to `input.to_string() + " world!"`
9
+
format!("{input} world!")
10
+
}
11
+
12
+
fn replace_me(input: &str) -> String {
13
+
input.replace("cars", "balloons")
14
+
}
15
+
1
16
fn main() {
2
-
// DON'T EDIT THIS SOLUTION FILE!
3
-
// It will be automatically filled after you finish the exercise.
17
+
// You can optionally experiment here.
18
+
}
19
+
20
+
#[cfg(test)]
21
+
mod tests {
22
+
use super::*;
23
+
24
+
#[test]
25
+
fn trim_a_string() {
26
+
assert_eq!(trim_me("Hello! "), "Hello!");
27
+
assert_eq!(trim_me(" What's up!"), "What's up!");
28
+
assert_eq!(trim_me(" Hola! "), "Hola!");
29
+
}
30
+
31
+
#[test]
32
+
fn compose_a_string() {
33
+
assert_eq!(compose_me("Hello"), "Hello world!");
34
+
assert_eq!(compose_me("Goodbye"), "Goodbye world!");
35
+
}
36
+
37
+
#[test]
38
+
fn replace_a_string() {
39
+
assert_eq!(
40
+
replace_me("I think cars are cool"),
41
+
"I think balloons are cool",
42
+
);
43
+
assert_eq!(
44
+
replace_me("I love to look at cars"),
45
+
"I love to look at balloons",
46
+
);
47
+
}
4
48
}
+36
-2
solutions/09_strings/strings4.rs
+36
-2
solutions/09_strings/strings4.rs
···
1
+
fn string_slice(arg: &str) {
2
+
println!("{arg}");
3
+
}
4
+
5
+
fn string(arg: String) {
6
+
println!("{arg}");
7
+
}
8
+
1
9
fn main() {
2
-
// DON'T EDIT THIS SOLUTION FILE!
3
-
// It will be automatically filled after you finish the exercise.
10
+
string_slice("blue");
11
+
12
+
string("red".to_string());
13
+
14
+
string(String::from("hi"));
15
+
16
+
string("rust is fun!".to_owned());
17
+
18
+
// Here, both answers work.
19
+
// `.into()` converts a type into an expected type.
20
+
// If it is called where `String` is expected, it will convert `&str` to `String`.
21
+
string("nice weather".into());
22
+
// But if it is called where `&str` is expected, then `&str` is kept `&str` since no conversion is needed.
23
+
// If you remove the `#[allow(…)]` line, then Clippy will tell you to remove `.into()` below since it is a useless conversion.
24
+
#[allow(clippy::useless_conversion)]
25
+
string_slice("nice weather".into());
26
+
27
+
string(format!("Interpolation {}", "Station"));
28
+
29
+
// WARNING: This is byte indexing, not character indexing.
30
+
// Character indexing can be done using `s.chars().nth(INDEX)`.
31
+
string_slice(&String::from("abc")[0..1]);
32
+
33
+
string_slice(" hello there ".trim());
34
+
35
+
string("Happy Monday!".replace("Mon", "Tues"));
36
+
37
+
string("mY sHiFt KeY iS sTiCkY".to_lowercase());
4
38
}