README for this exercise.
input.chars()
returns an iterator that we manually iterate through using .next()
.
Later in the program we take advantage of .iter() and the turbofish to reuse the same code across two different use cases.
Also note that an alternative solution is possible where the character uppercase function returns an iterator that is then collected()
Chris Biscardi: [0:00] In iterators2, we have a couple of things to do. The first one is that we need to complete the capitalized_first function. This will pass the first two tests, which capitalize Hello, and deal with empty strings.
[0:11] Then we'll have to finish these two TODOs in test three and test four. Because we can't really progress to the rest errors without filling in this additional code, we'll comment out test three and test four so we can work on test one and test two.
[0:24] Our first issue is that there's no method named "collect" or type "char" in the current scope, specifically at first.collect. This is because first is a character which we split out using chars here and get using the next() function.
[0:37] We can turn this into a string so that we can concatenate it with the rest of C by using two strings. Note that we aren't currently capitalizing Hello in our test, but the first converted to a string will also uppercase that string and then concatenate it to the rest of the string.
[0:52] Let's uncomment test three. We now need to use iterators to take the words vec and turn it into a Vec<String> that are capitalized. In this case, "Hello" and "World".
[1:01] We can do this using the .iterator function, which returns an iterator. We'll then map over the iterator and capitalize each string using our capitalized_first function.
[1:10] Finally, we need to collect the iterator. When we collect the iterator, we need to turn it into a type. In this case, we've specified capitalized_words as a Vec<Strings>. That's what we'll collect into.
[1:21] If we uncomment test four, we can see that we need to do a very similar thing with words, "hello," " ," "world," where we end up with a string, "Hello World" instead of an array, "Hello," "World." Luckily, the code that we just used in test three also works for tests four if we change the type signature.
[1:39] Note that instead of putting the String type on capitalized_words, we can also do what's called a turbofish. This is a turbofish which allows us to specify the type that we're collecting into of capitalized_words as a location that we call collect.