简体   繁体   English

为什么在使用 .await 或在像 `join!` 这样的宏中会出现“不满足 trait bound Future”?

[英]Why do I get "the trait bound Future is not satisfied" when using .await or in a macro like `join!`?

I'm working on an asynchronous Rust program but the join!我正在开发一个异步 Rust 程序,但是join! macro doesn't work.宏不起作用。 .await also does not work. .await也不起作用。 Where is my problem ?我的问题在哪里?

I saw examples in the official documentation .我在官方文档中看到了示例。

This also doesn't work:这也不起作用:

#[async_std::main]
async fn main() {}

I didn't use Tokio or Hyper because its a simple program.我没有使用 Tokio 或 Hyper,因为它是一个简单的程序。

use async_std::task;
use futures::executor::block_on;
use futures::join;
use futures::stream::{FuturesUnordered, StreamExt};
use rand::distributions::{Distribution, Uniform};
use std::thread;
use std::time::{Duration, Instant};

fn main() {
    let start = "bename Allah";
    println!("{}", start);

    fn fibonacci(n: u64) -> u64 {
        if n <= 1 {
            return 1;
        } else {
            return fibonacci(n - 1) + fibonacci(n - 2);
        }
    }

    fn fib(n: u64) {
        println!("Its : {}", fibonacci(n));
    }

    async fn calculate() {
        let do1 = fib(45);
        let do2 = fib(20);

        join!(do1, do2);
    }

    calculate();
    //i used block_on(calculate()) but got same error :(
}
[dependencies]
futures = "0.3.1"
rand = "0.7"
async-std = { version = "1.2", features = ["attributes"] }
surf = "1.0"

I get this error:我收到此错误:

error[E0277]: the trait bound `(): core::future::future::Future` is not satisfied
  --> src\main.rs:28:15
   |
28 |         join!(do1,do2);
   |               ^^^ the trait `core::future::future::Future` is not implemented for `()`
   | 
  ::: C:\Users\Mahdi\.cargo\registry\src\github.com-1ecc6299db9ec823\futures-util-0.3.4\src\future\maybe_done.rs:42:24
   |
42 | pub fn maybe_done<Fut: Future>(future: Fut) -> MaybeDone<Fut> {
   |                        ------ required by this bound in `futures_util::future::maybe_done::maybe_done`

Your problem can be reduced to this:您的问题可以简化为:

fn alpha() {}

async fn example() {
    alpha().await;
}
error[E0277]: the trait bound `(): std::future::Future` is not satisfied
  --> src/lib.rs:4:5
   |
4  |     alpha().await;
   |     ^^^^^^^^^^^^^ the trait `std::future::Future` is not implemented for `()`

You are attempting to await on something that does not implement a Future .您正在尝试等待未实现Future东西。 The return type of alpha is () . alpha的返回类型是() You likely intended to make your functions async :您可能打算使您的功能async

async fn alpha() {}
//^^^

async fn example() {
    alpha().await;
}

See also:也可以看看:

It's worth pointing out that computing a Fibonacci number is not a good fit for asynchronous work.值得指出的是,计算斐波那契数并不适合异步工作。 See also:也可以看看:

shepmaster are so right !楼主太对了! i found a new solution to do it correctly :我找到了一个新的解决方案来正确地做到这一点:

use std::thread;
fn main() {
let start = "bename Allah";
println!("{}", start);

fn fibonacci(n: u64) -> u64 {
    if n <= 1 {
        return 1;
    } else {
        return fibonacci(n - 1) + fibonacci(n - 2);
    }
}

fn fib(n: u64) {
    println!("Its : {}", fibonacci(n));
}

fn calculate() {
    let do1 = thread::spawn (move || {
        fib(45);
    });
    let do2 = thread::spawn (move || {
        fib(20);
    });
    do1.join();
    do2.join();
}

calculate();

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不使用“等待”的情况下获得未来的结果 - how get the result of Future without using “await” 为什么在 Result 上使用 match 语句会出现“预期类型 Future”错误? - Why do I get “expected type Future” error using match statement on Result? 如何正确使用“等待使用”语法? - How do I get the "await using" syntax correct? 特性绑定`tokio::net::tcp::stream::TcpStream: tokio_io::async_read::AsyncRead` 不满足 - the trait bound `tokio::net::tcp::stream::TcpStream: tokio_io::async_read::AsyncRead` is not satisfied 使用“Async”时,为什么使用“Await” - When using "Async", why "Await" is used 为什么我收到未处理的 Promise 拒绝等待 Promise.all - Why do I get an Unhandled Promise Rejection with await Promise.all 为什么当我执行 Await.result 时,我的 Scala 异步测试从未完成? - Why my Scala Async test never completes when I do Await.result? 为什么我在使用 async/await 时会收到多个响应? - Why am I getting multiple responses when I'm using async/await? 未使用Await的另一个Future.map中的Future [Future [T]]到Future [T]的未来? - Future[Future[T]] to Future[T] within another Future.map without using Await? 我如何在 dart (Flutter) 中使用 Future 和 .then() 转换这个简单的 async,await 格式的代码? - How can i Convert this simple async,await formatted code using Future and .then() in dart (Flutter)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM