为什么在结果上使用匹配语句会出现 "expected type Future" 错误?

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

我正在尝试在外部 crate 中使用函数,它应该是 return 函数签名所暗示的 Result<T, E> 结构:

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

我正在尝试按照 Rust 文档中的说明使用 match 语句解压 Result<T, E>,但由于某种原因我收到此错误:

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<_, _>`

Cargo.toml 中使用此 crate 的依赖项是: tastyworks = "0.13"

您尝试使用的函数是一个 async,因此您需要为其生成一个异步任务或在异步上下文中 运行 它。您需要 tokio(或另一个异步后端):

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),
    }
}

检查一些有趣的东西