知识卡片
闭包调用约束的三层
FnOnce 允许消耗闭包,FnMut 允许通过可变接收者反复调用,Fn 允许通过共享接收者反复调用。正文若移出非 Copy 捕获值,通常只能实现 FnOnce。move 声明的是捕获方式,若拥有的值在调用时只被借用,闭包仍可能实现 Fn。
相关卡片 [[move 闭包持有数据]]
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- 函数式语言特性:迭代器与闭包 Moving Captured Values Out of Closures and the Fn Traits · 查看原书依据
The way a closure captures and handles values from the environment affects which traits the closure implements, and traits are how functions and structs can specify what kinds of closures they can use. Closures will automatically implement one, two, or all three of these Fn traits, in an additive fashion, depending on how the closure’s body handles the values:
参考来源
- 位置:《The Rust Programming Language(第2版)》第13章 函数式语言特性:迭代器与闭包(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c13.xhtml`,块 0067)
- 结论依据:原文按闭包正文是否移出或修改捕获值解释可实现的三类 trait。
- 原始内容:The way a closure captures and handles values from the environment affects which traits the closure implements, and traits are how functions and structs can specify what kinds of closures they can use. Closures will automatically implement one, two, or all three of these Fn traits, in an additive fashion, depending on how the closure’s body handles the values: