How to use Future

Future is update from 0.1.0 to 0.3.0
how can i use it ?

i want to use and_then and from_err fucntions.

In futures 0.3, these functions are on the futures::future::TryFutureExt trait. Import it like so to use them on the new futures:

use futures::future::TryFutureExt;

If you want all the utility functions, I would recommend importing them all at once, like so:

use futures::prelude::*;

This brings in TryFuturesExt, as well as FuturesExt and StreamExt and a few other useful utility traits.


If you're updating from futures 0.1 to futures 0.3, this blog post might be useful: https://www.ncameron.org/blog/migrating-a-crate-from-futures-0-1-to-0-3/

Note that with async/await, there are other ways to write those functions:

// before
fut_a.and_then(|res| make_future(res))

// after
async move {
    let res = fut_a.await;
    make_future(res).await
}
// before
future::from_err(error)

// after
async move { Err(err) }
    pub async fn get_mining_info(&self) -> Result<MiningInfoResponse, FetchError> {

        let res = self.inner

            .get(self.uri_for("burst"))

            .headers((*self.headers).clone())

            .query(&GetMiningInfoRequest {

                request_type: &"getMiningInfo",

            })

            .send()

            .await

            .map_err(|e| FetchError::from(e))?

            .json::<MiningInfoResponse>()

            .await

            .map_err(|e| FetchError::from(e));

        res

    }

#[derive(Debug)]

pub enum FetchError {

    Http(reqwest::Error),

    Pool(PoolError),

}

i am confused with the return result.
there are two errors which one is failed to send http with reqwest::Error
and i another get wrong http back.
but in async i don't know how to parse the second error.

You would handle errors in the same way as in sync code.

i don't know how to realize it, can you help me ?

If you want help, please edit your posts to not have empty lines all over the place.

1 Like