知识卡片

普通读书笔记卡 · 1395.b

闭包把环境带入行为

理解等级:中级

闭包既有可调用代码,也携带其使用的外部环境。切片逐元素变换若需amount_to_add,普通函数不能直接捕获这个局部变量,闭包可以。带捕获闭包通常不能转为裸fn指针,因为指针本身没有存放环境的地方;非异步且不捕获环境的闭包可按匹配签名转换。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

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

      Closures The bare function pointers are limiting, because the only inputs available to the invoked function are those that are explicitly passed as parameter values. For example, consider some code that modifies every element of a slice using a function pointer: // In real code, an `Iterator` method would be more appropriate. pub fn modify_all(data: &mut [u3

    参考来源

    - 位置:《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-015)。 - 结论依据:原文在“Closures”附近的定义、例子或代码支撑本卡核心说明:闭包既有可调用代码,也携带其使用的外部环境。边界按已读前提解释;示例未执行。 - 原始内容:Closures The bare function pointers are limiting, because the only inputs available to the invoked function are those that are explicitly passed as parameter values. For example, consider some code that modifies every element of a slice using a function pointer: // In real code, an `Iterator` method would be more appropriate. pub fn modify_all(data: &mut [u3