asynchronousrusterror-handling

Why do I get "expected type Future" error using match statement on Result?


I'm trying to use a function in an external crate, it is supposed to return a Result<T, E> struct as implied by the function's signature:

pub async fn market_metrics(symbols: &[String]) -> Result<Vec<market_metrics::Item>, ApiError>

I'm trying to unpack the Result<T, E> with a match statement as instructed in Rust's documentation, but I am getting this error for some reason:

use tastyworks::market_metrics;

fn main() {
    let symbols = &[String::from("AAPL")];
    let m = market_metrics(symbols);
    match m {
        Ok(v) => v,
        Err(e) => panic!(e),
    }
}
error[E0308]: mismatched types
  --> src/main.rs:7:9
   |
7  |         Ok(v) => v,
   |         ^^^^^ expected opaque type, found enum `std::result::Result`
   | 
  ::: /home/user/.cargo/registry/src/github.com-1ecc6299db9ec823/tastyworks-0.13.0/src/lib.rs:79:52
   |
79 | pub async fn market_metrics(symbols: &[String]) -> Result<Vec<market_metrics::Item>, ApiError> {
   |                                                    ------------------------------------------- the `Output` of this `async fn`'s expected opaque type
   |
   = note: expected opaque type `impl std::future::Future`
                     found enum `std::result::Result<_, _>`

The dependency in Cargo.toml to use this crate is: tastyworks = "0.13"


Solution

  • The function you are trying to use is an async so you need to spawn an async task for it or run it in an async context. You need tokio (or another async backend) for it:

    use tastyworks::market_metrics;
    use tokio;
    
    #[tokio::main]
    async fn main() {
        let symbols = &[String::from("AAPL")];
        let m = market_metrics(symbols).await;
        match m {
            Ok(v) => v,
            Err(e) => panic!(e),
        }
    }
    

    Check some interesting related answers