@@ -1963,6 +1963,42 @@ pub trait Iterator {
19631963 } ) . break_value ( )
19641964 }
19651965
1966+ /// Applies function to the elements of iterator and returns
1967+ /// the first non-none result or the first error.
1968+ ///
1969+ ///
1970+ /// # Examples
1971+ ///
1972+ /// ```
1973+ /// let a = ["1", "2", "lol", "NaN", "5"];
1974+ ///
1975+ /// let result = a.iter().find_result(|&s| s.parse()? == 2);
1976+ ///
1977+ /// assert_eq!(result, Ok(Some(&2)));
1978+ /// ```
1979+ ///
1980+ /// ```
1981+ /// let a = ["1", "2", "lol", "NaN", "5"];
1982+ ///
1983+ /// let result = a.iter().find_result(|&s| s.parse()? == 5);
1984+ ///
1985+ /// assert!(result.is_err());
1986+ /// ```
1987+ #[ inline]
1988+ #[ unstable( feature = "find_result" , reason = "new API" , issue = "?" ) ]
1989+ fn find_result < F , E > ( & mut self , mut f : F ) -> Result < Option < Self :: Item > , E > where
1990+ Self : Sized ,
1991+ F : FnMut ( & Self :: Item ) -> Result < bool , E > ,
1992+ {
1993+ self . try_for_each ( move |x| {
1994+ match f ( & x) {
1995+ Ok ( false ) => LoopState :: Continue ( ( ) ) ,
1996+ Ok ( true ) => LoopState :: Break ( Ok ( x) ) ,
1997+ Err ( x) => LoopState :: Break ( Err ( x) ) ,
1998+ }
1999+ } ) . break_value ( ) . transpose ( )
2000+ }
2001+
19662002 /// Searches for an element in an iterator, returning its index.
19672003 ///
19682004 /// `position()` takes a closure that returns `true` or `false`. It applies
0 commit comments