rust - 为什么我需要克隆一个移动的变量?
问题描述
为什么我需要克隆tx
?我移动了它。我不能再在我的主线程中使用它了。
use std::sync::mpsc::{channel, Sender};
fn main() {
let (tx, _rx) = channel();
std::thread::spawn(move || {
takes_closure(|| Foo { sender: tx });
});
}
struct Foo {
sender: Sender<()>,
}
fn takes_closure(_: impl FnMut() -> Foo) {}
这有错误:
error[E0507]: cannot move out of captured variable in an `FnMut` closure
--> src/main.rs:7:40
|
4 | let (tx, _rx) = channel();
| -- captured outer variable
...
7 | takes_closure(|| Foo { sender: tx });
| ^^ cannot move out of captured variable in an `FnMut` closure
解决方案
推荐阅读
- c# - ASP Net Core 3.1 MVC 中的级联下拉菜单
- r - 如何计算数据框块的累积平均值,直到 R 中的每一行
- php - 在 PHP 中隐藏警告的机制是什么?
- java - 带有重试建议的 Jms 消息失败将响应发送到失败通道并再次传递到成功通道
- javascript - 从 PHP 或 JS 直接打印 PDF
- html - 通过 vbscript 与 Edge/HTML 交互
- r - 使用 R 中的间接标准化的年龄标准化率
- python - 如何根据python中的一些平面分割3D中的一些点
- typescript - 表达没有一千个选项的对象状态,同时避免“从不”问题
- javascript - 如何从 octokit api 获得更多结果?