add is_none_or · model-checking/verify-rust-std@b482e06 (original) (raw)

Original file line number Diff line number Diff line change
@@ -654,6 +654,32 @@ impl Option {
654 654 !self.is_some()
655 655 }
656 656
657 +/// Returns `true` if the option is a [`None`] or the value inside of it matches a predicate.
658 + ///
659 + /// # Examples
660 + ///
661 + /// ```
662 + /// #![feature(is_none_or)]
663 + ///
664 + /// let x: Option = Some(2);
665 + /// assert_eq!(x.is_none_or(|x
666 + ///
667 + /// let x: Option = Some(0);
668 + /// assert_eq!(x.is_none_or(|x
669 + ///
670 + /// let x: Option = None;
671 + /// assert_eq!(x.is_none_or(|x
672 + /// ```
673 + #[must_use]
674 +#[inline]
675 +#[unstable(feature = "is_none_or", issue = "none")]
676 +pub fn is_none_or(self, f: impl FnOnce(T) -> bool) -> bool {
677 +match self {
678 +None => true,
679 +Some(x) => f(x),
680 +}
681 +}
682 +
657 683 /////////////////////////////////////////////////////////////////////////
658 684 // Adapter for working with references
659 685 /////////////////////////////////////////////////////////////////////////