From my forum question:
Consider an iterator that does two things:
- Read data from a data source.
- Perform a complex calculation on the data read.
This means that next is computationally expensive, but skip could be cheap if
implemented by hand. It could just do step 1 and throw the data away without doing
I have tried various ways to do this [… but] I cannot find a public API to create a Skip except by calling Iterator::skip.
Is this impossible?
It’s easy, according to @scottmcm: implement my own
Iterator::nth method, and I will get a
skip implementation for free.
In retrospect it makes sense, but it was not obvious to me at all just from reading the docs. It is not mentioned in the Implementing Iterators section of the API, or the docs for either method. And as the link in my post suggests, trying to work my way around the ownership of the APIs led me quite far afield.
Admittedly it’s not the most common case, but I think it would save others time if the standard library at least mentioned this somewhere.
EDIT: fixed forum link which changed its name when I marked it solved.