diff --git a/library/core/src/option.rs b/library/core/src/option.rs index 8adfb6f4bcf..d9ee289f216 100644 --- a/library/core/src/option.rs +++ b/library/core/src/option.rs @@ -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 = Some(2); + /// assert_eq!(x.is_some_with(|x| x > 1), true); + /// + /// let x: Option = Some(0); + /// assert_eq!(x.is_some_with(|x| x > 1), false); + /// + /// let x: Option = 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