【发布时间】:2016-12-02 15:49:24
【问题描述】:
我正在实现一个 Rust 应用程序,其中结构向量将被传递到闭包中。
#[derive(Clone, Copy)]
struct AttackInfo {
ppid: usize,
pname: &'static str,
cpu: f32,
}
type AList = Vec<AttackInfo>;
fn Task() {
let mut list: AList = AList::new();
// ...
// I fill up the list with many items
// ...
output.put(|msg| *msg = Some(Message::Value(list)));
}
output.put 是一个带有闭包的函数,这是由库定义的。
由于list 变量的错误,我无法发送列表的值:
cannot move out of captured outer variable in an `FnMut` closure
闭包会被执行多次,直到列表中的所有值都被清空。如何在不丢失变量范围的情况下发送列表数据?
【问题讨论】:
-
闭包会被执行多次,直到列表中的所有值都被清空。如何在不丢失变量范围的情况下发送列表数据。 => 你是什么意思?查看编写的代码,我希望
list被完全消耗一次(一次)。 -
我的意思是函数
put会读出向量中的所有数据。当然,该功能可以一次性完成。 -
用大写字母命名函数不是惯用的 Rust。函数和变量使用
snake_case。