Iterator::advance (again)

Starting a new topic because [Pre-RFC] Iter advance expired.

I like this proposal and think that it should be considered. Right now rust has only one way to advance an iterator without materializing every element: .nth(). However .nth() still materializes one element. It seems like a weird choose to me to make .nth() the fundamental method and implement .skip() ontop of it instead of the other way around. This seems to not only make Skip necessary and relatively complicated but also forces the materialization as described earlier.

If we had .advance(), .nth() could simply be .advance(n).next() and skip is just kept for compatibility.

The major downside is that we "missed" the boat to do this originally, and now the docs say to override .nth(). However I don't think this is a major issue as the default impl of .advance() could be .nth(count - 1). Then we can slowly upgrade types by implementing .advance() directly and falling back to the default impl of .nth().

Right now if you want to avoid materializing unnecessary elements you need to do quite some gymnastics by storing the number you want to skip and doing it on the next read. Of course this doesn't even work if you want to call other iterator methods like .size_hint() that may give different results even if you manually adjust for the "holdback".

This has since been implemented!

4 Likes

Wow. That's the best type of news!