Add Option::is_some_with.
This commit is contained in:
parent
9ad5d82f82
commit
282224edf1
1 changed files with 21 additions and 0 deletions
|
@ -551,6 +551,27 @@ impl<T> Option<T> {
|
|||
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…
Add table
Reference in a new issue