【发布时间】:2020-06-28 06:15:12
【问题描述】:
我想复制将闭包/函数作为参数的行为和人体工程学,就像map 所做的那样:iterator.map(|x| ...)。
我注意到一些库代码允许传入异步功能,但这种方法不允许我传入参数:
pub fn spawn<F, T>(future: F) -> JoinHandle<T>
where
F: Future<Output = T> + Send + 'static,
T: Send + 'static,
spawn(async { foo().await });
我希望做以下事情之一:
iterator.map(async |x| {...});
async fn a(x: _) {}
iterator.map(a)
【问题讨论】:
-
我认为你需要一个 Stream 来实现这一点,看看 crate futures
-
@MarioSantini docs.rs/async-std/1.5.0/async_std/stream/… 他们接受的函数似乎并不明显,而是最终结果似乎是异步的。
-
我想说的是:如果您需要对集合进行异步迭代,您可能需要一个流,这是您可以迭代的功能。
标签: asynchronous rust async-await closures future