iterators2.rs (1423B)
1 // In this exercise, you'll learn some of the unique advantages that iterators 2 // can offer. 3 4 // TODO: Complete the `capitalize_first` function. 5 // "hello" -> "Hello" 6 fn capitalize_first(input: &str) -> String { 7 let mut chars = input.chars(); 8 match chars.next() { 9 None => String::new(), 10 Some(first) => todo!(), 11 } 12 } 13 14 // TODO: Apply the `capitalize_first` function to a slice of string slices. 15 // Return a vector of strings. 16 // ["hello", "world"] -> ["Hello", "World"] 17 fn capitalize_words_vector(words: &[&str]) -> Vec<String> { 18 // ??? 19 } 20 21 // TODO: Apply the `capitalize_first` function again to a slice of string 22 // slices. Return a single string. 23 // ["hello", " ", "world"] -> "Hello World" 24 fn capitalize_words_string(words: &[&str]) -> String { 25 // ??? 26 } 27 28 fn main() { 29 // You can optionally experiment here. 30 } 31 32 #[cfg(test)] 33 mod tests { 34 use super::*; 35 36 #[test] 37 fn test_success() { 38 assert_eq!(capitalize_first("hello"), "Hello"); 39 } 40 41 #[test] 42 fn test_empty() { 43 assert_eq!(capitalize_first(""), ""); 44 } 45 46 #[test] 47 fn test_iterate_string_vec() { 48 let words = vec!["hello", "world"]; 49 assert_eq!(capitalize_words_vector(&words), ["Hello", "World"]); 50 } 51 52 #[test] 53 fn test_iterate_into_string() { 54 let words = vec!["hello", " ", "world"]; 55 assert_eq!(capitalize_words_string(&words), "Hello World"); 56 } 57 }