知识卡片

普通读书笔记卡 · 1395.b.1

闭包能力取决于值的使用

理解等级:中级

FnOnce允许消耗闭包调用,所有闭包都实现它;不移出捕获值的闭包还可实现FnMut,不修改也不移出的还可实现Fn。move说明怎样捕获,不等于只能调用一次:拥有捕获值并只读取它的闭包仍可多次调用。Fn也不保证没有外部副作用或内部可变性。

原书将调用能力类比捕获方式时作了简化;这里按Rust Reference调用trait说明限定,2026-10-27查阅。

这张卡片看不懂?

已有反馈 · 0 / 10 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • Chapter 1. Types Closures · 查看原书依据

      FnOnce Describes a closure that can be called only once. If some part of the environment is moved into the closure’s context, and the closure’s body subsequently moves it out of the closure’s context, then those moves can happen only once—there’s no other copy of the source item to move from—and so the closure can be invoked only once. FnMut Describes a clos

    参考来源

    - 位置:《Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)》第1章《Closures》(spine5;extracted/Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)/_epub-src/OEBPS/ch01.html;u018-ch01-016)。 - 结论依据:原文在“FnOnce”附近的定义、例子或代码支撑本卡核心说明:FnOnce允许消耗闭包调用,所有闭包都实现它;不移出捕获值的闭包还可实现FnMut,不修改也不移出的还可实现Fn。边界按已读前提解释;示例未执行。 - 原始内容:FnOnce Describes a closure that can be called only once. If some part of the environment is moved into the closure’s context, and the closure’s body subsequently moves it out of the closure’s context, then those moves can happen only once—there’s no other copy of the source item to move from—and so the closure can be invoked only once. FnMut Describes a clos