Though we have nice Iterator::collect(), the most common use cases of this method as I’ve seen are my_iter.collect::<Vec<_>>(), which is a bit complicated to type. So it would be worth to add this simple shortcut method for it.
collect_vec seems to be one of the more popular methods in itertools (if popularity feelings tell us anything). Best methods from itertools should move into libstd somehow.
A change of this calibre (it’s a small new feature) should be eligible to just be filed as an implementation in a PR to Rust and the libs team can discuss inclusion as an unstable feature. However before that:
This doesn’t seem to be feasible to add to Iterator, because the Iterator trait is in libcore, and can’t use Vec which is in a higher crate (libcollections). Do you see any way to get around this?
Maybe we could turn it around and add Vec::from_iter()? That could get around the core/std issue at least.
I don’t know how collect() works today, but it would sure be nice if, for Iterators that know about their size, the Vec could allocate to the required capacity in one go.
Vec::from_iter exists, and its exactly how collect works, in fact. Vec implements FromIterator, and collect has the signature fn collect<T>(self) -> T where T: FromIterator<Self::Item>.
Return type polymorphism is a powerful advantage of the trait system over similar kinds of polymorphism & collect is the most common ‘entry point’ to understanding that idea - that you can collect into vectors or hashmaps or strings with the same method. I haven’t found the turbofish too annoying, but YMMV.
Personally, I love the method collect() for the same reason that @withoutboats wrote. I think every rust programmer take a look at how exactly .collect() works. But since many of us type .collect::<Vec<_>>() everyday and feels uncomfortable, it would be nice to add a sugar method for this common use case.
Anyway @sgrif 's idea seems promising to me. Is there some other case like this in std?
I don't know how collect() works today, but it would sure be nice if, for Iterators that know about their size, the Vec could allocate to the required capacity in one go.