Iterator::collect converts an iterator into a collection such as
which typically requires an allocation. You should avoid calling
the collection is then only iterated over again.
For this reason, it is often better to return an iterator type like
impl Iterator<Item=T> from a function than a
Vec<T>. Note that sometimes
additional lifetimes are required on these return types, as this post
Finally, when you write an iterator it is often worth implementing the
ExactSizeIterator::len method, if possible.
extend calls that use the iterator may then do fewer
allocations, because they have advance information about the number of elements
yielded by the iterator.