Fix capitalization in iterators2.rs

This commit is contained in:
Rock070 2024-01-10 00:40:15 +08:00
parent 2c4dc73771
commit 9247e98c37

View File

@ -6,8 +6,6 @@
// Execute `rustlings hint iterators2` or use the `hint` watch subcommand for a
// hint.
// I AM NOT DONE
// Step 1.
// Complete the `capitalize_first` function.
// "hello" -> "Hello"
@ -15,7 +13,7 @@ pub fn capitalize_first(input: &str) -> String {
let mut c = input.chars();
match c.next() {
None => String::new(),
Some(first) => ???,
Some(first) => first.to_string().to_uppercase() + &input[1..],
}
}
@ -24,7 +22,12 @@ pub fn capitalize_first(input: &str) -> String {
// Return a vector of strings.
// ["hello", "world"] -> ["Hello", "World"]
pub fn capitalize_words_vector(words: &[&str]) -> Vec<String> {
vec![]
let mut new_vec: Vec<String> = vec![];
for word in words.iter() {
new_vec.push(capitalize_first(word))
}
new_vec
}
// Step 3.
@ -32,7 +35,12 @@ pub fn capitalize_words_vector(words: &[&str]) -> Vec<String> {
// Return a single string.
// ["hello", " ", "world"] -> "Hello World"
pub fn capitalize_words_string(words: &[&str]) -> String {
String::new()
let mut new_string = String::new();
for word in words.iter() {
new_string.push_str(&capitalize_first(word))
}
new_string
}
#[cfg(test)]