我正在努力从try_for_each_concurrent
调用的闭包中获取共享数据:
use futures::channel::oneshot;
use std::sync::Arc;
use futures::stream::{self, StreamExt, TryStreamExt};
fn main() {
let arc_val = Arc::new(5);
let (tx1, rx1) = oneshot::channel();
let (tx2, rx2) = oneshot::channel();
let (_tx3, rx3) = oneshot::channel();
let stream = stream::iter(vec![rx1, rx2, rx3]);
let future =
stream
.map(Ok)
.try_for_each_concurrent(5, |channels: oneshot::Receiver<u32>| async move {
let arc_ref = Arc::clone(&arc_val);
use_arc(arc_ref).await
});
}
async fn use_arc(arc_ref: Arc<u32>) -> Result<(), String> {
println!("Value {}", arc_ref);
Ok(())
}
错误:
error[E0507]: cannot move out of `arc_val`, a captured variable in an `FnMut` closure
--> src/main.rs:17:87
|
7 | let arc_val = Arc::new(5);
| ------- captured outer variable
...
17 | .try_for_each_concurrent(5, |channels: oneshot::Receiver<u32>| async move {
| _______________________________________________________________________________________^
18 | | let arc_ref = Arc::clone(&arc_val);
| | -------
| | |
| | move occurs because `arc_val` has type `std::sync::Arc<u32>`, which does not implement the `Copy` trait
| | move occurs due to use in generator
19 | | use_arc(arc_ref).await
20 | | });
| |_____________^ move out of `arc_val` occurs here
发生的错误是我实际遇到的错误的概括版本,但是解决该错误应该可以解决我的问题。