File tree 1 file changed +27
-0
lines changed
1 file changed +27
-0
lines changed Original file line number Diff line number Diff line change @@ -597,6 +597,33 @@ impl<T> Option<T> {
597
597
!self . is_some ( )
598
598
}
599
599
600
+ /// Returns `true` if the option is a [`None`] or the [`Some`] value
601
+ /// inside of it matches a predicate.
602
+ ///
603
+ /// # Examples
604
+ ///
605
+ /// ```
606
+ /// #![feature(is_some_with)]
607
+ ///
608
+ /// let x: Option<u32> = Some(2);
609
+ /// assert_eq!(x.is_none_or(|&x| x > 1), true);
610
+ ///
611
+ /// let x: Option<u32> = Some(0);
612
+ /// assert_eq!(x.is_none_or(|&x| x > 1), false);
613
+ ///
614
+ /// let x: Option<u32> = None;
615
+ /// assert_eq!(x.is_none_or(|&x| x > 1), true);
616
+ /// ```
617
+ #[ must_use]
618
+ #[ inline]
619
+ #[ unstable( feature = "is_some_with" , issue = "93050" ) ]
620
+ pub fn is_none_or ( & self , f : impl FnOnce ( & T ) -> bool ) -> bool {
621
+ match self {
622
+ Some ( x) => f ( x) ,
623
+ None => true ,
624
+ }
625
+ }
626
+
600
627
/////////////////////////////////////////////////////////////////////////
601
628
// Adapter for working with references
602
629
/////////////////////////////////////////////////////////////////////////
You can’t perform that action at this time.
0 commit comments