mirror of
https://github.com/rust-lang/rust.git
synced 2026-05-15 12:39:31 +03:00
Add Option::is_some_with.
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user