Skip to content

I don't understand why iterators2 works #2133

Closed Answered by frroossst
egolep asked this question in Q&A
Discussion options

You must be logged in to vote

I am by no means a rust expert, but here is my best attempt at an explanation.

If you look at the implementation of the .collect() in iterator ref, you'll notice a <B> in the function definition, which means it is a generic. You can learn more about generics here.

Basically, the collect() method is implemented for Vec<String> and String and for many other types. So, when yo do the capitalize_words_vector the compiler infers that the variant of the collect method to be used must be the one that returns Vec<String> and in the function capitalize_words_string would use the variant or version of the collect method that returns the type String

Hint: you'll notice that the reduce method is also…

Replies: 4 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Answer selected by mo8it
Comment options

You must be logged in to vote
1 reply
@mo8it
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants
Converted from issue

This discussion was converted from issue #2129 on October 16, 2024 10:59.