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

Hello,

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?

Thanks!

Best, Oliver

find() can help:

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

However, there are lots of possibilities.

Another option is:

iter.flatten().find(P).ok_or(default_err)

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