知识卡片
move 闭包持有数据
move || … 按值捕获环境。把非 Copy 的向量移入传给 thread::spawn 的闭包后,创建者不能再使用原值,闭包可在新线程中持有它。若环境里捕获的值本身是引用,搬移该引用不会延长被引用对象的寿命。
相关卡片 [[闭包按需捕获变量]]
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- 函数式语言特性:迭代器与闭包 Capturing References or Moving Ownership · 查看原书依据
We spawn a new thread, giving the thread a closure to run as an argument. The closure body prints out the list. In Listing 13-4, the closure only captured list using an immutable reference because that’s the least amount of access to list needed to print it.
参考来源
- 位置:《The Rust Programming Language(第2版)》第13章 函数式语言特性:迭代器与闭包(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c13.xhtml`,块 0063)
- 结论依据:线程例子说明即使正文只读取,外部对象寿命仍可能不足。
- 原始内容:We spawn a new thread, giving the thread a closure to run as an argument. The closure body prints out the list. In Listing 13-4, the closure only captured list using an immutable reference because that’s the least amount of access to list needed to print it.