Add Option::is_some_with.
This commit is contained in:
parent
9ad5d82f82
commit
282224edf1
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user