Combining futures
Combining, and chaining, our futures allows us to perform multiple operations in sequential order and helps organize our code a bit more. They can be used to transform, splice, filter, and so on <Self as Future>::Item
s.
How to do it...
- Inside the
bin
folder, create a new file calledcombinators.rs
.
- Add the following code and run it with
cargo run --bin combinators
:
1 extern crate futures; 2 extern crate futures_util; 3 4 use futures::prelude::*; 5 use futures::channel::{mpsc, oneshot}; 6 use futures::executor::block_on; 7 use futures::future::{ok, err, join_all, select_all, poll_fn}; 8 use futures::stream::iter_result; 9 use futures_util::stream::select_all as select_all_stream; 10 11 use std::thread; 12 13 const FINISHED: Result<Async<()>, Never> = Ok(Async::Ready(()));
- Let's add our
join_all
example function:
15 fn join_all_example() { 16 let future1 = Ok::<_, ()>(vec![1, 2, 3]); 17 let future2 = Ok(vec![10, 20, 30]...