@@ -542,6 +542,29 @@ impl<T, E> Result<T, E> {
542
542
matches ! ( * self , Ok ( _) )
543
543
}
544
544
545
+ /// Returns `true` if the result is [`Ok`] wrapping a value matching the predicate.
546
+ ///
547
+ /// # Examples
548
+ ///
549
+ /// ```
550
+ /// #![feature(is_some_with)]
551
+ ///
552
+ /// let x: Result<u32, &str> = Ok(2);
553
+ /// assert_eq!(x.is_ok_with(|&x| x > 1), true);
554
+ ///
555
+ /// let x: Result<u32, &str> = Ok(0);
556
+ /// assert_eq!(x.is_ok_with(|&x| x > 1), false);
557
+ ///
558
+ /// let x: Result<u32, &str> = Err("hey");
559
+ /// assert_eq!(x.is_ok_with(|&x| x > 1), false);
560
+ /// ```
561
+ #[ must_use]
562
+ #[ inline]
563
+ #[ unstable( feature = "is_some_with" , issue = "93050" ) ]
564
+ pub fn is_ok_with ( & self , f : impl FnOnce ( & T ) -> bool ) -> bool {
565
+ matches ! ( self , Ok ( x) if f( x) )
566
+ }
567
+
545
568
/// Returns `true` if the result is [`Err`].
546
569
///
547
570
/// # Examples
@@ -563,6 +586,30 @@ impl<T, E> Result<T, E> {
563
586
!self . is_ok ( )
564
587
}
565
588
589
+ /// Returns `true` if the result is [`Err`] wrapping a value matching the predicate.
590
+ ///
591
+ /// # Examples
592
+ ///
593
+ /// ```
594
+ /// #![feature(is_some_with)]
595
+ /// use std::io::{Error, ErrorKind};
596
+ ///
597
+ /// let x: Result<u32, Error> = Err(Error::new(ErrorKind::NotFound, "!"));
598
+ /// assert_eq!(x.is_err_with(|x| x.kind() == ErrorKind::NotFound), true);
599
+ ///
600
+ /// let x: Result<u32, Error> = Err(Error::new(ErrorKind::PermissionDenied, "!"));
601
+ /// assert_eq!(x.is_err_with(|x| x.kind() == ErrorKind::NotFound), false);
602
+ ///
603
+ /// let x: Result<u32, Error> = Ok(123);
604
+ /// assert_eq!(x.is_err_with(|x| x.kind() == ErrorKind::NotFound), false);
605
+ /// ```
606
+ #[ must_use]
607
+ #[ inline]
608
+ #[ unstable( feature = "is_some_with" , issue = "93050" ) ]
609
+ pub fn is_err_with ( & self , f : impl FnOnce ( & E ) -> bool ) -> bool {
610
+ matches ! ( self , Err ( x) if f( x) )
611
+ }
612
+
566
613
/////////////////////////////////////////////////////////////////////////
567
614
// Adapter for each variant
568
615
/////////////////////////////////////////////////////////////////////////
0 commit comments