mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-07 11:19:18 +00:00
Complete strings exercises
This commit is contained in:
parent
3c0941fc2c
commit
b63f6a6a9e
@ -5,7 +5,6 @@
|
|||||||
// Execute `rustlings hint strings1` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint strings1` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let answer = current_favorite_color();
|
let answer = current_favorite_color();
|
||||||
@ -13,5 +12,6 @@ fn main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn current_favorite_color() -> String {
|
fn current_favorite_color() -> String {
|
||||||
"blue"
|
// "blue".to_string() // can be used to make strings from different types (aka integers, floats, chars, etc.)
|
||||||
|
String::from("blue") // can only be used to make strings from &string(string slices)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,11 +5,12 @@
|
|||||||
// Execute `rustlings hint strings2` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint strings2` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// 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) {
|
|
||||||
|
// This needs to be a string slice because the function is expecting a string slice.
|
||||||
|
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.");
|
||||||
|
|||||||
@ -3,21 +3,31 @@
|
|||||||
// Execute `rustlings hint strings3` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint strings3` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
fn trim_me(input: &str) -> String {
|
fn trim_me(input: &str) -> String {
|
||||||
// TODO: Remove whitespace from both ends of a string!
|
// TODO: Remove whitespace from both ends of a string!
|
||||||
???
|
input.trim().to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn compose_me(input: &str) -> String {
|
fn compose_me(input: &str) -> String {
|
||||||
// TODO: Add " world!" to the string! There's multiple ways to do this!
|
// TODO: Add " world!" to the string! There's multiple ways to do this!
|
||||||
???
|
|
||||||
|
// let mut new_string = String::new();
|
||||||
|
// new_string.push_str(input);
|
||||||
|
// new_string.push_str(" world!");
|
||||||
|
// new_string
|
||||||
|
|
||||||
|
format!("{input} {}", "world!")
|
||||||
|
|
||||||
|
// let a = input.to_string();
|
||||||
|
// let b = " world!".to_string();
|
||||||
|
// a + &b
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn replace_me(input: &str) -> String {
|
fn replace_me(input: &str) -> String {
|
||||||
// TODO: Replace "cars" in the string with "balloons"!
|
// TODO: Replace "cars" in the string with "balloons"!
|
||||||
???
|
input.replace("cars", "balloons")
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|||||||
@ -7,8 +7,6 @@
|
|||||||
//
|
//
|
||||||
// No hints this time!
|
// No hints this time!
|
||||||
|
|
||||||
// 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("red".to_string());
|
||||||
???(String::from("hi"));
|
string(String::from("hi"));
|
||||||
???("rust is fun!".to_owned());
|
string("rust is fun!".to_owned()); // this is cloning
|
||||||
???("nice weather".into());
|
string_slice("nice weather".into()); // calls it self
|
||||||
???(format!("Interpolation {}", "Station"));
|
string(format!("Interpolation {}", "Station"));
|
||||||
???(&String::from("abc")[0..1]);
|
string_slice(&String::from("abc")[0..1]);
|
||||||
???(" hello there ".trim());
|
string_slice(" hello there ".trim());
|
||||||
???("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());
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user