@@ -40,6 +40,7 @@ mod skip;
4040mod skip_while;
4141mod step_by;
4242mod take;
43+ mod try_for_each;
4344mod zip;
4445
4546use all:: AllFuture ;
@@ -52,6 +53,7 @@ use fold::FoldFuture;
5253use min_by:: MinByFuture ;
5354use next:: NextFuture ;
5455use nth:: NthFuture ;
56+ use try_for_each:: TryForEeachFuture ;
5557
5658pub use chain:: Chain ;
5759pub use filter:: Filter ;
@@ -66,6 +68,7 @@ pub use zip::Zip;
6668
6769use std:: cmp:: Ordering ;
6870use std:: marker:: PhantomData ;
71+ use std:: ops:: Try ;
6972
7073use cfg_if:: cfg_if;
7174
@@ -921,6 +924,52 @@ extension_trait! {
921924 Skip :: new( self , n)
922925 }
923926
927+ #[ doc = r#"
928+ Applies a falliable function to each element in a stream, stopping at first error and returning it.
929+
930+ # Examples
931+
932+ ```
933+ # fn main() { async_std::task::block_on(async {
934+ #
935+ use std::collections::VecDeque;
936+ use std::sync::mpsc::channel;
937+ use async_std::prelude::*;
938+
939+ let (tx, rx) = channel();
940+
941+ let s: VecDeque<usize> = vec![1, 2, 3].into_iter().collect();
942+ let s = s.try_for_each(|v| {
943+ if v % 2 == 1 {
944+ tx.clone().send(v).unwrap();
945+ Ok(())
946+ } else {
947+ Err("even")
948+ }
949+ });
950+
951+ let res = s.await;
952+ drop(tx);
953+ let values: Vec<_> = rx.iter().collect();
954+
955+ assert_eq!(values, vec![1]);
956+ assert_eq!(res, Err("even"));
957+ #
958+ # }) }
959+ ```
960+ "# ]
961+ fn try_for_each<F , R >(
962+ self ,
963+ f: F ,
964+ ) -> impl Future <Output = R > [ TryForEeachFuture <Self , F , Self :: Item , R >]
965+ where
966+ Self : Sized ,
967+ F : FnMut ( Self :: Item ) -> R ,
968+ R : Try <Ok = ( ) >,
969+ {
970+ TryForEeachFuture :: new( self , f)
971+ }
972+
924973 #[ doc = r#"
925974 'Zips up' two streams into a single stream of pairs.
926975
0 commit comments