Changed iter::Extendable
and iter::FromIterator
to take a Iterator
by value.
#13039
+75
−78
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Changed
iter::Extendable
anditer::FromIterator
to take aIterator
by value.These functions always exhaust the passed
Iterator
, and are often used for transferring the values of a newIterator
directly into a data structure, so using them usually require the use of the&mut
operator:This patch changes both the
FromIterator
andExtendable
traits to take the iterator by value instead, which makes the common case of using these traits less heavy:Composability
This technically makes the traits less flexible from a type system pov, because they now require ownership.
However, because
Iterator
provides theByRef
adapter, there is no loss of functionality:Motivation
This change makes it less painful to use iterators for shuffling values around between collections, which makes it more acceptable to always use them for this, enabling more flexibility.
For example,
foo.extend(bar.move_iter())
can generally be the fastest way to append an collections content to another one, without both needing to have the same type. Making this easy to use would allow the removal of special cased methods likepush_all()
on vectors. (See #12456)I opened #13038 as well, to discuss this change in general if people object to it.
Further work
This didn't change the
collect()
method to take by valueself
, nor any of the other adapters that also exhaust their iterator argument. For consistency this should probably happen in the long term, but for now this is too much trouble, as every use of them would need to be checked for accidentally changed semantic by going&mut self -> self
. (which allows for the possibility that aPod
iterator got copied instead of exhausted without generating a type error by the change)