Rust 程序设计语言 简体中文版 1.85.0
height: 12 }, ]; let mut sort_operations = vec![]; let value = String::from("closure called"); list.sort_by_key(|r| { sort_operations.push(value); r.width }); value(一个 String)插入 sort_operations vector 来实现计数。闭包捕获了 value,然后通过将 value 的所有权转移给 sort_operations vector 的方式将其移出闭包。这个闭包只能被调用一次;尝试第二次调用它将无法工作,因为 这时 value 已经不在闭包的环境中,无法被再次插入 sort_operations 中!因而,这个闭包只 实现了 FnOn sort_by_key(|r| { | --- captured by this `FnMut` closure 18 | sort_operations.push(value); | ^^^^^ move occurs because `value` has type `String`0 码力 | 562 页 | 3.23 MB | 8 天前3
共 1 条
- 1