add is_none_or
This commit is contained in:
parent
bbe9a9c20b
commit
63bdcaa2d9
1 changed files with 26 additions and 0 deletions
|
@ -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
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue