My question is: Am I missing something built-in that does this already? If not, should/could the code be implemented in a better way?
PS: I'm aware of FromIterator, however I'd like to have an Iterator of inner values (not Result's) to work with while not having to collect them first. Perhaps I'm missing something obvious here.
This looks nice. I don't think there's any simple replacement for it in the standard library. The itertools crate has process_results which has a different design but might work for some of the same use cases.
My example code probably didn't illustrate the problem well, but I'd like to pass the iterator of non-Result values elsewhere, while still saving the error.
It would be great to have an adapter that did impl Iterator<Item = Result<T, E>> → Result<impl Iterator<Item = T>, E>. But unfortunately that just can't be implemented elegantly.
The core problem is that you must iterate the whole iterator to find out whether it needs to be an Err, but for the iterator to produce them later, it needs to store those items somewhere, at which point this is just collect.
What you're doing looks like it's fairly similar to what collect-into-Result does internally:
But I don't think the library ever exposes that iterator directly, right now.