知识卡片

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

按调用需要约束闭包

理解等级:中级

接收回调时,按实际调用方式选择约束:至多调用一次可用FnOnce,需要反复调用并允许更新环境可用FnMut,只持共享访问调用时用Fn。能满足Fn的闭包也满足FnMut与FnOnce,反向不成立。逐元素调用的modify_all可用FnMut,并把回调绑定声明为mut,以允许其更新捕获状态。

这张卡片看不懂?

已有反馈 · 0 / 10 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

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

      FnMut(u32) -> u32, { for value in data { *value = mutator(*value); } } Rust has three different Fn* traits, which between them express some distinctions around this environment-capturing behavior: 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 m

    参考来源

    - 位置:《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-017)。 - 结论依据:原文在“FnMut”附近的定义、例子或代码支撑本卡核心说明:接收回调时,按实际调用方式选择约束:至多调用一次可用FnOnce,需要反复调用并允许更新环境可用FnMut,只持共享访问调用时用Fn。边界按已读前提解释;示例未执行。 - 原始内容:FnMut(u32) -> u32, { for value in data { *value = mutator(*value); } } Rust has three different Fn* traits, which between them express some distinctions around this environment-capturing behavior: 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 m