Skip to content

Commit

Permalink
Rollup merge of rust-lang#126328 - RalfJung:is_none_or, r=workingjubilee
Browse files Browse the repository at this point in the history
Add Option::is_none_or

ACP: rust-lang/libs-team#212
  • Loading branch information
workingjubilee authored Jun 13, 2024
2 parents 17c90d8 + b482e06 commit 9921cd2
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions core/src/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,32 @@ impl<T> Option<T> {
!self.is_some()
}

/// Returns `true` if the option is a [`None`] or the value inside of it matches a predicate.
///
/// # Examples
///
/// ```
/// #![feature(is_none_or)]
///
/// let x: Option<u32> = Some(2);
/// assert_eq!(x.is_none_or(|x| x > 1), true);
///
/// let x: Option<u32> = Some(0);
/// assert_eq!(x.is_none_or(|x| x > 1), false);
///
/// let x: Option<u32> = None;
/// assert_eq!(x.is_none_or(|x| x > 1), true);
/// ```
#[must_use]
#[inline]
#[unstable(feature = "is_none_or", issue = "none")]
pub fn is_none_or(self, f: impl FnOnce(T) -> bool) -> bool {
match self {
None => true,
Some(x) => f(x),
}
}

/////////////////////////////////////////////////////////////////////////
// Adapter for working with references
/////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit 9921cd2

Please sign in to comment.