Idiomatic way for p: T -> bool, x: Option<T>

match x {
  None => false,
  Some(x) => p(x),
}

Is there a more idiomatic way to express this? I am saying: the object exists and it satisfies predicate.

I think that would be x.filter(p).is_some() ? With a as_ref in front to avoid consuming maybe.

optx.map_or(false, f)

4 Likes

This is very clever, but imho not as easy to read as @RustyYato 's .map_or

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.