加入
联接操作会等待一组 Future 全部就绪,然后返回它们的结果集合。这类似于 JavaScript 中的 Promise.al
或 Python 中的 asyncio.gather
。
Speaker Notes
将此示例复制到准备好的 src/main.rs
文件中,并从该文件运行它。
-
对于多个类型不相交的 Future,可以使用
std::future::join!
进行处理,但必须要确定在编译时 Future 的数量。目前,可在futures
crate 中使用该功能,但很快也会在std::future
中正式发布。 -
The risk of
join
is that one of the futures may never resolve, this would cause your program to stall. -
还可以将
join_all
与join!
结合使用,并行处理所有对 http 服务的请求和数据库查询。尝试使用futures::join!
将tokio::time::sleep
添加到 Future 中。这不是一个超时操作(其需要使用select!
,下一章会详细介绍),而是展示了join!
的使用方式。