小编And*_*nov的帖子

在 Rust 中克隆用于异步移动闭包的字符串

有什么方法可以克隆async move闭包的值吗futures::stream

我正在使用未来的for_each_concurrent

我的代码是这样的:

async fn foo() {
    use futures::stream::{self, StreamExt};

    let a: String = String::new();

    let stream = stream::once(async { 42 });

    stream
        .for_each_concurrent(None, |stream_item| async move {
            println!("{}", a);
            println!("{}", stream_item);
        })
        .await;
}
Run Code Online (Sandbox Code Playgroud)

这里的错误:

error[E0507]: cannot move out of `a`, a captured variable in an `FnMut` closure
  --> src/main.rs:9:61
   |
4  |       let a: String = String::new();
   |           - captured outer variable
...
9  |           .for_each_concurrent(None, |stream_item| async move {
   |  _____________________________________________________________^ …
Run Code Online (Sandbox Code Playgroud)

asynchronous future stream rust

5
推荐指数
1
解决办法
3678
查看次数

标签 统计

asynchronous ×1

future ×1

rust ×1

stream ×1