Skip to content

Commit 1d3e6a7

Browse files
committed
Add Option::is_none_or
1 parent 4033686 commit 1d3e6a7

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

library/core/src/option.rs

+27
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,33 @@ impl<T> Option<T> {
597597
!self.is_some()
598598
}
599599

600+
/// Returns `true` if the option is a [`None`] or the [`Some`] value
601+
/// inside of it matches a predicate.
602+
///
603+
/// # Examples
604+
///
605+
/// ```
606+
/// #![feature(is_some_with)]
607+
///
608+
/// let x: Option<u32> = Some(2);
609+
/// assert_eq!(x.is_none_or(|&x| x > 1), true);
610+
///
611+
/// let x: Option<u32> = Some(0);
612+
/// assert_eq!(x.is_none_or(|&x| x > 1), false);
613+
///
614+
/// let x: Option<u32> = None;
615+
/// assert_eq!(x.is_none_or(|&x| x > 1), true);
616+
/// ```
617+
#[must_use]
618+
#[inline]
619+
#[unstable(feature = "is_some_with", issue = "93050")]
620+
pub fn is_none_or(&self, f: impl FnOnce(&T) -> bool) -> bool {
621+
match self {
622+
Some(x) => f(x),
623+
None => true,
624+
}
625+
}
626+
600627
/////////////////////////////////////////////////////////////////////////
601628
// Adapter for working with references
602629
/////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)