diff --git a/library/core/src/option.rs b/library/core/src/option.rs index bca73cb770fbb..e1e52a1b47a2d 100644 --- a/library/core/src/option.rs +++ b/library/core/src/option.rs @@ -597,6 +597,33 @@ impl Option { !self.is_some() } + /// Returns `true` if the option is a [`None`] or the [`Some`] value + /// inside of it matches a predicate. + /// + /// # Examples + /// + /// ``` + /// #![feature(is_some_with)] + /// + /// let x: Option = Some(2); + /// assert_eq!(x.is_none_or(|&x| x > 1), true); + /// + /// let x: Option = Some(0); + /// assert_eq!(x.is_none_or(|&x| x > 1), false); + /// + /// let x: Option = None; + /// assert_eq!(x.is_none_or(|&x| x > 1), true); + /// ``` + #[must_use] + #[inline] + #[unstable(feature = "is_some_with", issue = "93050")] + pub fn is_none_or(&self, f: impl FnOnce(&T) -> bool) -> bool { + match self { + Some(x) => f(x), + None => true, + } + } + ///////////////////////////////////////////////////////////////////////// // Adapter for working with references /////////////////////////////////////////////////////////////////////////