3

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"

kmdreko
  • 42,554
  • 6
  • 57
  • 106
orie
  • 541
  • 6
  • 20

1 Answers1

4

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

Netwave
  • 40,134
  • 6
  • 50
  • 93
  • Thanks for the quick response, I'll more about async, meanwhile I tried to run your code and im getting this `failed to resolve: could not find main in tokio could not find main in tokio rustc(E0433) main.rs(4, 10): could not find `main` in tokio and main function is not allowed to be async` `main function is not allowed to be async rustc(E0752) main.rs(5, 1): main function is not allowed to be async` – orie Mar 15 '21 at 15:58
  • @orie, you probably miss the `tokio = { version = "1", features = ["full"] }` in your tokio cargo entry. – Netwave Mar 15 '21 at 16:00
  • that helped but now it produces a tokio-related issue `thread 'main' panicked at 'not currently running on a Tokio 0.2.x runtime.', /Users/or/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-0.2.25/src/runtime/handle.rs:118:28` – orie Mar 15 '21 at 16:11
  • 1
    That means you have to use `tokio = { version = "0.2" }` too, since multiple major versions of Tokio can't be used together. – Cerberus Mar 15 '21 at 16:23
  • Adding to this: pulling in `tokio` with `features = ["full"]` probably gives you a bunch of functionality that neither you nor the dependency require. You might get away with `features = ["macros", "rt-core"]` or `features = ["macros", "rt-threaded"]`, depending on whether you want to use the single- or multi-threaded runtime. – sebpuetz Mar 15 '21 at 16:36
  • changing to `tokio = { version = "0.2", features = ["full"] }` worked, that you all for the support! – orie Mar 15 '21 at 16:45