Macro core::future::join

source ·
pub macro join( $($fut:expr),+ $(,)? ) {
    ...
}
🔬This is a nightly-only experimental API. (future_join #91642)
Expand description

同时轮询多个 futures,完成后,返回所有结果的元组。

虽然 join!(a, b).await 类似于 (a.await, b.await),但 join! 会同时轮询两个 futures,因此效率更高。

Examples

#![feature(future_join)]

use std::future::join;

async fn one() -> usize { 1 }
async fn two() -> usize { 2 }

let x = join!(one(), two()).await;
assert_eq!(x, (1, 2));
Run

join! 的参数是可变的,因此您可以传递任意数量的 futures:

#![feature(future_join)]

use std::future::join;

async fn one() -> usize { 1 }
async fn two() -> usize { 2 }
async fn three() -> usize { 3 }

let x = join!(one(), two(), three()).await;
assert_eq!(x, (1, 2, 3));
Run