我有有一个对象的矢量resolve()
的方法,它使用reqwest
来查询外部web API。在resolve()
每个对象上调用该方法后,我想打印每个请求的结果。
这是我编译和工作的半异步代码(但不是真正异步的):
for mut item in items {
item.resolve().await;
item.print_result();
}
Run Code Online (Sandbox Code Playgroud)
我试图用来tokio::join!
产生所有异步调用并等待它们完成,但我可能做错了什么:
tokio::join!(items.iter_mut().for_each(|item| item.resolve()));
Run Code Online (Sandbox Code Playgroud)
这是我得到的错误:
error[E0308]: mismatched types
--> src\main.rs:25:51
|
25 | tokio::join!(items.iter_mut().for_each(|item| item.resolve()));
| ^^^^^^^^^^^^^^ expected `()`, found opaque type
|
::: src\redirect_definition.rs:32:37
|
32 | pub async fn resolve(&mut self) {
| - the `Output` of this `async fn`'s found opaque type
|
= note: expected unit type `()`
found opaque type `impl std::future::Future`
Run Code Online (Sandbox Code Playgroud)
如何一次调用resolve()
所有实例的方法?
这段代码反映了答案——现在我正在处理我不太理解的借用检查器错误——我应该用 …