From d947bcd7be6a23aade1e347b40537a9fc5b0de5f Mon Sep 17 00:00:00 2001 From: why <435371447@qq.com> Date: Mon, 8 Apr 2024 20:47:12 +0800 Subject: [PATCH] update --- exercises/iterators/iterators2.rs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/exercises/iterators/iterators2.rs b/exercises/iterators/iterators2.rs index dda82a0..b2b3bb5 100644 --- a/exercises/iterators/iterators2.rs +++ b/exercises/iterators/iterators2.rs @@ -6,7 +6,7 @@ // Execute `rustlings hint iterators2` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE + // Step 1. // Complete the `capitalize_first` function. @@ -15,7 +15,12 @@ pub fn capitalize_first(input: &str) -> String { let mut c = input.chars(); match c.next() { None => String::new(), - Some(first) => ???, + Some(first) => { + let mut result = String::new(); + result.push(first.to_uppercase().next().unwrap()); + result.push_str(c.as_str()); + result + } } } @@ -24,7 +29,7 @@ pub fn capitalize_first(input: &str) -> String { // Return a vector of strings. // ["hello", "world"] -> ["Hello", "World"] pub fn capitalize_words_vector(words: &[&str]) -> Vec { - vec![] + words.iter().map(|&word| capitalize_first(word)).collect() } // Step 3. @@ -32,7 +37,8 @@ pub fn capitalize_words_vector(words: &[&str]) -> Vec { // Return a single string. // ["hello", " ", "world"] -> "Hello World" pub fn capitalize_words_string(words: &[&str]) -> String { - String::new() + words.iter().map(|&word| capitalize_first(word)).collect::>().join("") + } #[cfg(test)]