预期的 `async` 块,发现了不同的 `async` 块
Expected `async` block, found a different `async` block
我正在尝试使用 futures::future::select_ok
(playground):
use std::time::Duration;
use tokio; // 1.16.1
use futures; // 0.3.19
#[tokio::main]
async fn main() {
let first_future = async {
tokio::time::sleep(Duration::from_secs(1)).await;
Ok(3)
};
let second_future = async {
tokio::time::sleep(Duration::from_millis(100)).await;
Err(())
};
let third_future = async {
tokio::time::sleep(Duration::from_secs(300)).await;
Ok(3)
};
futures::future::select_ok(&[first_future,second_future.await,third_future]).await;
}
遇到错误:
error[E0308]: mismatched types
--> src/main.rs:19:47
|
7 | let first_future = async {
| ______________________________-
8 | | tokio::time::sleep(Duration::from_secs(1)).await;
9 | | Ok(3)
10 | | };
| |_____- the expected `async` block
...
19 | futures::future::select_ok(&[first_future,second_future.await,third_future]).await;
| ^^^^^^^^^^^^^^^^^^^ expected opaque type, found enum `Result`
|
= note: expected opaque type `impl futures::Future<Output = [async output]>`
found enum `Result<_, ()>`
For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` due to previous error
我不知道我在这里遗漏了什么,非常感谢任何帮助。
每个异步块都被视为不同的类型,就像闭包 (IIRC) 一样。它们都实现了 Future
,因此您可能希望实际动态调度它们。为此,您需要将它们包装在 Pin<Box>
:
中
use futures;
use std::future::Future;
use std::pin::Pin;
use std::time::Duration;
use tokio; // 1.16.1 // 0.3.19
#[tokio::main]
async fn main() {
let first_future = async {
tokio::time::sleep(Duration::from_secs(1)).await;
Ok(3)
};
let second_future = async {
tokio::time::sleep(Duration::from_millis(100)).await;
Err(())
};
let third_future = async {
tokio::time::sleep(Duration::from_secs(300)).await;
Ok(3)
};
let futures: [Pin<Box<dyn Future<Output = Result<usize, ()>>>>; 3] = [
Box::pin(first_future),
Box::pin(second_future),
Box::pin(third_future),
];
futures::future::select_ok(futures).await;
}
我正在尝试使用 futures::future::select_ok
(playground):
use std::time::Duration;
use tokio; // 1.16.1
use futures; // 0.3.19
#[tokio::main]
async fn main() {
let first_future = async {
tokio::time::sleep(Duration::from_secs(1)).await;
Ok(3)
};
let second_future = async {
tokio::time::sleep(Duration::from_millis(100)).await;
Err(())
};
let third_future = async {
tokio::time::sleep(Duration::from_secs(300)).await;
Ok(3)
};
futures::future::select_ok(&[first_future,second_future.await,third_future]).await;
}
遇到错误:
error[E0308]: mismatched types
--> src/main.rs:19:47
|
7 | let first_future = async {
| ______________________________-
8 | | tokio::time::sleep(Duration::from_secs(1)).await;
9 | | Ok(3)
10 | | };
| |_____- the expected `async` block
...
19 | futures::future::select_ok(&[first_future,second_future.await,third_future]).await;
| ^^^^^^^^^^^^^^^^^^^ expected opaque type, found enum `Result`
|
= note: expected opaque type `impl futures::Future<Output = [async output]>`
found enum `Result<_, ()>`
For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` due to previous error
我不知道我在这里遗漏了什么,非常感谢任何帮助。
每个异步块都被视为不同的类型,就像闭包 (IIRC) 一样。它们都实现了 Future
,因此您可能希望实际动态调度它们。为此,您需要将它们包装在 Pin<Box>
:
use futures;
use std::future::Future;
use std::pin::Pin;
use std::time::Duration;
use tokio; // 1.16.1 // 0.3.19
#[tokio::main]
async fn main() {
let first_future = async {
tokio::time::sleep(Duration::from_secs(1)).await;
Ok(3)
};
let second_future = async {
tokio::time::sleep(Duration::from_millis(100)).await;
Err(())
};
let third_future = async {
tokio::time::sleep(Duration::from_secs(300)).await;
Ok(3)
};
let futures: [Pin<Box<dyn Future<Output = Result<usize, ()>>>>; 3] = [
Box::pin(first_future),
Box::pin(second_future),
Box::pin(third_future),
];
futures::future::select_ok(futures).await;
}