Add Option::is_some_with.

This commit is contained in:
Mara Bos 2022-01-18 22:17:34 +01:00
parent 9ad5d82f82
commit 282224edf1

View File

@ -551,6 +551,27 @@ pub const fn is_some(&self) -> bool {
matches!(*self, Some(_))
}
/// Returns `true` if the option is a [`Some`] wrapping a value matching the predicate.
///
/// # Examples
///
/// ```
/// let x: Option<u32> = Some(2);
/// assert_eq!(x.is_some_with(|x| x > 1), true);
///
/// let x: Option<u32> = Some(0);
/// assert_eq!(x.is_some_with(|x| x > 1), false);
///
/// let x: Option<u32> = None;
/// assert_eq!(x.is_some_with(|x| x > 1), false);
/// ```
#[must_use]
#[inline]
#[unstable(feature = "is_some_with", issue = "none")]
pub fn is_some_with(&self, f: impl FnOnce(&T) -> bool) -> bool {
matches!(self, Some(x) if f(x))
}
/// Returns `true` if the option is a [`None`] value.
///
/// # Examples