complete: strings

This commit is contained in:
seth 2021-06-22 14:45:51 +08:00
parent 26374eb122
commit 192725b336
3 changed files with 12 additions and 18 deletions

View file

@ -7,8 +7,6 @@
// you think each value is. That is, add either `string_slice` or `string` // 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! // before the parentheses on each line. If you're right, it will compile!
// I AM NOT DONE
fn string_slice(arg: &str) { fn string_slice(arg: &str) {
println!("{}", arg); println!("{}", arg);
} }
@ -17,14 +15,14 @@ fn string(arg: String) {
} }
fn main() { fn main() {
???("blue"); string_slice("blue");
???("red".to_string()); string_slice(" hello there ".trim());
???(String::from("hi")); string_slice(&String::from("abc")[0..1]);
???("rust is fun!".to_owned()); string("red".to_string());
???("nice weather".into()); string(String::from("hi"));
???(format!("Interpolation {}", "Station")); string("rust is fun!".to_owned());
???(&String::from("abc")[0..1]); string("nice weather".into());
???(" hello there ".trim()); string(format!("Interpolation {}", "Station"));
???("Happy Monday!".to_string().replace("Mon", "Tues")); string("Happy Monday!".to_string().replace("Mon", "Tues"));
???("mY sHiFt KeY iS sTiCkY".to_lowercase()); string("mY sHiFt KeY iS sTiCkY".to_lowercase());
} }

View file

@ -2,13 +2,11 @@
// Make me compile without changing the function signature! // Make me compile without changing the function signature!
// Execute `rustlings hint strings1` for hints ;) // Execute `rustlings hint strings1` for hints ;)
// I AM NOT DONE
fn main() { fn main() {
let answer = current_favorite_color(); let answer = current_favorite_color();
println!("My current favorite color is {}", answer); println!("My current favorite color is {}", answer);
} }
fn current_favorite_color() -> String { fn current_favorite_color() -> String {
"blue" String::from("red")
} }

View file

@ -2,11 +2,9 @@
// Make me compile without changing the function signature! // Make me compile without changing the function signature!
// Execute `rustlings hint strings2` for hints :) // Execute `rustlings hint strings2` for hints :)
// I AM NOT DONE
fn main() { fn main() {
let word = String::from("green"); // Try not changing this line :) let word = String::from("green"); // Try not changing this line :)
if is_a_color_word(word) { if is_a_color_word(&word) {
println!("That is a color word I know!"); println!("That is a color word I know!");
} else { } else {
println!("That is not a color word I know."); println!("That is not a color word I know.");