【发布时间】:2019-04-01 23:56:16
【问题描述】:
pub fn create_future(
notificator: mpsc::Sender<usize>,
proxy: Proxy,
) -> impl Future<Item = (), Error = ()> {
proxy.something()
.and_then(move |sub| {
sub.for_each(move |a| { // <---- Closure A
proxy.something_else(a)
.and_then(move |b| { // <---- Closure B
notificator.send(b.len()); // <---- Error!
Ok(())
})
.or_else(|e| {
panic!("oops {}", e);
Ok(())
})
})
})
.map_err(|e| {
()
})
}
这不会编译,因为
.and_then(move |b| {
^^^^^^^^^ cannot move out of captured outer variable in an `FnMut` closure
我对错误的理解是:
- 闭包 B 是
FnMut,它通过获取其所有权来捕获notificator - 在闭包 B 中,
send再次需要取得所有权 - 现在
send和 Closure B 都在修改notificator因此出现错误。
我的理解对吗?我该如何解决这个问题?
【问题讨论】:
-
什么是
Proxy?请问可以提供MCVE吗? -
“在闭包 B 中,再次发送需要取得所有权” - 这听起来不对。
mpsc::Sender::send接受&self。 -
我相信How can I move a captured variable into a closure within a closure?的答案已经回答了您的问题。如果您不同意,请edit您的问题解释差异。否则,我们可以将此问题标记为已回答。
标签: rust