@@ -2104,6 +2104,43 @@ pub trait Iterator {
21042104 self . try_fold ( ( ) , check ( f) ) . break_value ( )
21052105 }
21062106
2107+ /// Applies function to the elements of iterator and returns
2108+ /// the first non-none result or the first error.
2109+ ///
2110+ /// # Examples
2111+ ///
2112+ /// ```
2113+ /// #![feature(try_find)]
2114+ ///
2115+ /// let a = ["1", "2", "lol", "NaN", "5"];
2116+ ///
2117+ /// let is_my_num = |s: &str, search: i32| -> Result<bool, std::num::ParseIntError> {
2118+ /// Ok(s.parse::<i32>()? == search)
2119+ /// };
2120+ ///
2121+ /// let result = a.iter().try_find(|&&s| is_my_num(s, 2));
2122+ /// assert_eq!(result, Ok(Some(&"2")));
2123+ ///
2124+ /// let result = a.iter().try_find(|&&s| is_my_num(s, 5));
2125+ /// assert!(result.is_err());
2126+ /// ```
2127+ #[ inline]
2128+ #[ unstable( feature = "try_find" , reason = "new API" , issue = "63178" ) ]
2129+ fn try_find < F , E , R > ( & mut self , mut f : F ) -> Result < Option < Self :: Item > , E >
2130+ where
2131+ Self : Sized ,
2132+ F : FnMut ( & Self :: Item ) -> R ,
2133+ R : Try < Ok = bool , Error = E > ,
2134+ {
2135+ self . try_for_each ( move |x| match f ( & x) . into_result ( ) {
2136+ Ok ( false ) => LoopState :: Continue ( ( ) ) ,
2137+ Ok ( true ) => LoopState :: Break ( Ok ( x) ) ,
2138+ Err ( x) => LoopState :: Break ( Err ( x) ) ,
2139+ } )
2140+ . break_value ( )
2141+ . transpose ( )
2142+ }
2143+
21072144 /// Searches for an element in an iterator, returning its index.
21082145 ///
21092146 /// `position()` takes a closure that returns `true` or `false`. It applies
0 commit comments