[英]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.