You can create a type on which you can repeatedly call
next and have it return references which are only valid until the next iteration, because you’ll need to mutably borrow it again.
However, it is not currently possible to write a trait which abstracts over multiple such types, and you cannot implement the
std::iter::Iterator trait for such a type. Rust’s type system is not powerful enough (yet) to write such an abstraction.
To express this type of thing in Rust’s type system we would need higher kinded lifetimes, which is a feature Rust currently doesn’t have. There has been a lot of talk recently about adding such a feature, but the implications of that are very complicated and it will likely take quite a long time to get there.
Here’s an example: https://is.gd/36wzil
EDIT: here’s a link to the relevant discussion on “internals”. https://internals.rust-lang.org/t/blog-post-series-alternative-type-constructors-and-hkt/4300