Does anyone have an example of going from an Iterator of type Result<T, SomeError> to Iterator of type T, while handling all errors? I can see how this could be done with `for x in y` syntax, but I'm wondering if this can be done using combinators. Can it?
-
Show this thread
-
Replying to @yoshuawuyts
Sure, you can do anything with a fold that you could do with a loop, but it feels iffy to swallow errors in the return type.
1 reply 0 retweets 1 like -
Replying to @bodil
Oh, so I want to *handle* errors, not make them go away. E.g. return on the first error case, or continue going on. I guess the assumption here is that the source is not guaranteed to be finite, so returning on the first error seems like the most sane approach.
2 replies 0 retweets 0 likes -
Replying to @yoshuawuyts
Oh, you said "all" errors, so I assumed you weren't looking for https://doc.rust-lang.org/std/result/enum.Result.html#impl-FromIterator%3CResult%3CA%2C%20E%3E%3E … but now I think you are.
1 reply 0 retweets 1 like -
Replying to @yoshuawuyts @Argorak
Yes, literally if you have an iterator of Rssult<A, E> you can collect it into a Result<FromIterator<A>, E> (warning pseudocode but hopefully it makes sense) where on the first error it returns Err(E).
3 replies 0 retweets 4 likes -
ah yeah, that makes a lot of sense! Another question: does this buffer the incoming data until the underlying iterator is done? It does right?
1 reply 0 retweets 0 likes -
Replying to @yoshuawuyts @Argorak
Yeah, collect() does that p much by definition. You still need something to collect into, a Vec or smth.
1 reply 0 retweets 1 like
Ah yeah that makes a lot of sense. In the use case I'm thinking of the iterator may yield indefinitely, so I can't use collect it seems :( Thanks heaps for the thorough explanation tho, this has been very helpful!
Loading seems to be taking a while.
Twitter may be over capacity or experiencing a momentary hiccup. Try again or visit Twitter Status for more information.