Reputation: 70257
Rust has two main traits for iteration: the standard Iterator
for things that can be traversed in order, and DoubleEndedIterator
for things that can additionally be iterated from the back.
This makes sense to me. A lot of useful methods are defined on Iterator
, like map
, filter
, and whatnot. And then things that operate from the "back", so to speak, like rfind
and rfold
are defined on DoubleEndedIterator
since such operations only make sense on such iterators.
But then we've got Iterator::rev
fn rev(self) -> Rev<Self> where Self: Sized + DoubleEndedIterator, { Rev::new(self) }
rev
is a function defined on Iterator
but with an additional constraint on Self
that it be DoubleEndedIterator
as well.
What is the practical benefit to having this function defined on Iterator
? We can only call it on DoubleEndedIterator
implementors, and DoubleEndedIterator
extends Iterator
so we'll never end up in a situation where something implements the former and not the latter. So why is rev
not defined on DoubleEndedIterator
like rfind
and rfold
are?
Upvotes: 7
Views: 673
Reputation: 26697
This allow to:
Iterator
in one place.rev()
on an Iterator
without need to use DoubleEndedIterator
, the prelude
include it so it's not a problem actually. I would so add this reduce cognitive load by letting the user ignore DoubleEndedIterator
exist.Upvotes: 4