11// iterators2.rs
2- // In this module, you'll learn some of the unique advantages that iterators can offer.
3- // Step 1. Complete the `capitalize_first` function to pass the first two cases.
4- // Step 2. Apply the `capitalize_first` function to a vector of strings.
5- // Ensure that it returns a vector of strings as well.
6- // Step 3. Apply the `capitalize_first` function again to a list.
7- // Try to ensure it returns a single string.
2+ // In this exercise, you'll learn some of the unique advantages that iterators
3+ // can offer. Follow the steps to complete the exercise.
84// As always, there are hints if you execute `rustlings hint iterators2`!
95
106// I AM NOT DONE
117
8+ // Step 1.
9+ // Complete the `capitalize_first` function.
10+ // "hello" -> "Hello"
1211pub fn capitalize_first ( input : & str ) -> String {
1312 let mut c = input. chars ( ) ;
1413 match c. next ( ) {
1514 None => String :: new ( ) ,
16- Some ( first) => first . collect :: < String > ( ) + c . as_str ( ) ,
15+ Some ( first) => ??? ,
1716 }
1817}
1918
19+ // Step 2.
20+ // Apply the `capitalize_first` function to a slice of string slices.
21+ // Return a vector of strings.
22+ // ["hello", "world"] -> ["Hello", "World"]
23+ pub fn capitalize_words_vector ( words : & [ & str ] ) -> Vec < String > {
24+ vec ! [ ]
25+ }
26+
27+ // Step 3.
28+ // Apply the `capitalize_first` function again to a slice of string slices.
29+ // Return a single string.
30+ // ["hello", " ", "world"] -> "Hello World"
31+ pub fn capitalize_words_string ( words : & [ & str ] ) -> String {
32+ String :: new ( )
33+ }
34+
2035#[ cfg( test) ]
2136mod tests {
2237 use super :: * ;
2338
24- // Step 1.
25- // Tests that verify your `capitalize_first` function implementation
2639 #[ test]
2740 fn test_success ( ) {
2841 assert_eq ! ( capitalize_first( "hello" ) , "Hello" ) ;
@@ -33,18 +46,15 @@ mod tests {
3346 assert_eq ! ( capitalize_first( "" ) , "" ) ;
3447 }
3548
36- // Step 2.
3749 #[ test]
3850 fn test_iterate_string_vec ( ) {
3951 let words = vec ! [ "hello" , "world" ] ;
40- let capitalized_words: Vec < String > = // TODO
41- assert_eq ! ( capitalized_words, [ "Hello" , "World" ] ) ;
52+ assert_eq ! ( capitalize_words_vector( & words) , [ "Hello" , "World" ] ) ;
4253 }
4354
4455 #[ test]
4556 fn test_iterate_into_string ( ) {
4657 let words = vec ! [ "hello" , " " , "world" ] ;
47- let capitalized_words = // TODO
48- assert_eq ! ( capitalized_words, "Hello World" ) ;
58+ assert_eq ! ( capitalize_words_string( & words) , "Hello World" ) ;
4959 }
5060}
0 commit comments