Iterator<Item=Result<A, E>>: Find first Ok(a) for predicate


I have an Iterator<Item=Result<A, E>>, a predicate P for A and a default Err. I want to find the first element that is either an Ok(a) where a satisfies P or an Err, and then return either, or if neither can be found, return the default Err.

Are there any Iterator methods that can help, or should I just loop over elements?


Best, Oliver

find() can help:

iter.find(|item| match item {
    Ok(a) => P(a),
    Err(_) => false,

However, there are lots of possibilities.

Another option is:


Ah, thanks! I was somehow looking for a method special to Result, but now I see that a simple find() works well.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.