Ignoring those methods with default implementation, a iterator is just
pub trait Iterator {
type Item;
fn next(&mut self) -> Option<Self::Item>;
}
The problem of the above design, is that it is not able to limit the implementation from returning Some
after None
being returned. So we have FusedIterator
and fuse
as workaround.
But at least in theory, the Iterator
trait should looks like
pub trait Iterator {
type Item;
fn next(self) -> Option<(Self,Self::Item)>;
}
With this, once it returns None
no one can use the iterator any more as it is already moved out. If it returns Some
, you were given back the object itself, so you can continue iterating. Perfect!
However, this design does not work for today’s Rust
. The biggest problem is that it makes Iterator
not object safe. The second problem is passing self
by value prevents sending trait objects.
But in my mind, this API is just the same as the old one, up to some de-sugaring. So, are there any ideas to make the later API de-sugar to the API (but keep the addition restriction to implementation) above without affecting too much?