File tree Expand file tree Collapse file tree 1 file changed +21
-0
lines changed Expand file tree Collapse file tree 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> {
551551 matches ! ( * self , Some ( _) )
552552 }
553553
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+
554575 /// Returns `true` if the option is a [`None`] value.
555576 ///
556577 /// # Examples
You can’t perform that action at this time.
0 commit comments