What do you think about Iterator::single?

One might not be in control of the value source. It could be a stream, for example. There might also be multiple values involved in the whole process, but it ends with "and then get exactly one more item" for it to be correct.

I don't think I've personally come across enough usage for it to be a bother to not have it inside std. When I come across the situations I usually want some more custom behavior, so writing the "check the rest of the iterator" part isn't much of a burden.

However, if I were to use a std provided version, I'd probably prefer it to return a Result with an error value that provides me with a new iterator that I can use to inspect the wrong set of values for error reporting or fallbacks and such.

As example that would be having something like:

enum IteratorSingleError<I> {
    Empty,
    Many(IteratorSingleErrorIterator<I>),
}

and have IteratorSingleErrorIterator (I know, awful name) contain the first item, next item, and rest iterator, combining them into a full iterator again.

Maybe this would be a candidate for an itertools addition?

2 Likes