1
1
// 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.
8
4
// As always, there are hints if you execute `rustlings hint iterators2`!
9
5
10
6
// I AM NOT DONE
11
7
8
+ // Step 1.
9
+ // Complete the `capitalize_first` function.
10
+ // "hello" -> "Hello"
12
11
pub fn capitalize_first ( input : & str ) -> String {
13
12
let mut c = input. chars ( ) ;
14
13
match c. next ( ) {
15
14
None => String :: new ( ) ,
16
- Some ( first) => first . collect :: < String > ( ) + c . as_str ( ) ,
15
+ Some ( first) => ??? ,
17
16
}
18
17
}
19
18
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
+
20
35
#[ cfg( test) ]
21
36
mod tests {
22
37
use super :: * ;
23
38
24
- // Step 1.
25
- // Tests that verify your `capitalize_first` function implementation
26
39
#[ test]
27
40
fn test_success ( ) {
28
41
assert_eq ! ( capitalize_first( "hello" ) , "Hello" ) ;
@@ -33,18 +46,15 @@ mod tests {
33
46
assert_eq ! ( capitalize_first( "" ) , "" ) ;
34
47
}
35
48
36
- // Step 2.
37
49
#[ test]
38
50
fn test_iterate_string_vec ( ) {
39
51
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" ] ) ;
42
53
}
43
54
44
55
#[ test]
45
56
fn test_iterate_into_string ( ) {
46
57
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" ) ;
49
59
}
50
60
}
0 commit comments