File tree 1 file changed +21
-0
lines changed
1 file changed +21
-0
lines changed Original file line number Diff line number Diff line change @@ -551,6 +551,27 @@ impl<T> Option<T> {
551
551
matches ! ( * self , Some ( _) )
552
552
}
553
553
554
+ /// Returns `true` if the option is a [`Some`] wrapping a value matching the predicate.
555
+ ///
556
+ /// # Examples
557
+ ///
558
+ /// ```
559
+ /// let x: Option<u32> = Some(2);
560
+ /// assert_eq!(x.is_some_with(|x| x > 1), true);
561
+ ///
562
+ /// let x: Option<u32> = Some(0);
563
+ /// assert_eq!(x.is_some_with(|x| x > 1), false);
564
+ ///
565
+ /// let x: Option<u32> = None;
566
+ /// assert_eq!(x.is_some_with(|x| x > 1), false);
567
+ /// ```
568
+ #[ must_use]
569
+ #[ inline]
570
+ #[ unstable( feature = "is_some_with" , issue = "none" ) ]
571
+ pub fn is_some_with ( & self , f : impl FnOnce ( & T ) -> bool ) -> bool {
572
+ matches ! ( self , Some ( x) if f( x) )
573
+ }
574
+
554
575
/// Returns `true` if the option is a [`None`] value.
555
576
///
556
577
/// # Examples
You can’t perform that action at this time.
0 commit comments