Files
rustlings/exercises/09_strings/strings4.rs
T

35 lines
978 B
Rust
Raw Normal View History

2026-03-12 20:54:14 -04:00
fn string_slice(arg: &str) {
println!("{arg}");
}
fn string(arg: String) {
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() {
2026-03-23 03:36:33 -04:00
string_slice("blue"); // &str
2026-03-12 20:54:14 -04:00
2026-03-23 03:36:33 -04:00
string("red".to_string()); // String
2026-03-12 20:54:14 -04:00
2026-03-23 03:36:33 -04:00
string(String::from("hi")); // String
2026-03-12 20:54:14 -04:00
2026-03-23 03:36:33 -04:00
string("rust is fun!".to_owned()); // String
2026-03-12 20:54:14 -04:00
2026-03-23 03:36:33 -04:00
string("nice weather".into()); // String
2026-03-12 20:54:14 -04:00
2026-03-23 03:36:33 -04:00
string(format!("Interpolation {}", "Station")); // String
2026-03-12 20:54:14 -04:00
// WARNING: This is byte indexing, not character indexing.
// Character indexing can be done using `s.chars().nth(INDEX)`.
2026-03-23 03:36:33 -04:00
string_slice(&String::from("abc")[0..1]); // &str
2026-03-12 20:54:14 -04:00
2026-03-23 03:36:33 -04:00
string_slice(" hello there ".trim()); // &str
2026-03-12 20:54:14 -04:00
2026-03-23 03:36:33 -04:00
string("Happy Monday!".replace("Mon", "Tues")); // String
2026-03-12 20:54:14 -04:00
2026-03-23 03:36:33 -04:00
string("mY sHiFt KeY iS sTiCkY".to_lowercase()); // String
2026-03-12 20:54:14 -04:00
}