知识卡片
Cell通过值访问内部状态
Cell通过整体值操作提供内部可变性,避免交出可随意别名的内部引用。get要求T:Copy并复制出值;set接收一个新的T,不应误写成也要求Copy。它适合小型计数等状态,跨线程共享则还需满足同步约束。
签名限定:Cell官方文档,2026-10-27查阅。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Chapter 1. Types Smart Pointer Types · 查看原书依据
Cell<T>, relaxes the rule (Item 15) that an item can be mutated only by its owner or by code that holds the (only) mutable reference to the item. This interior mutability allows for greater flexibility—for example, allowing trait implementations that mutate internals even when the method signature allows only &self. However, it also incurs costs: as well as
参考来源
- 位置:《Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)》第1章《Smart Pointer Types》(spine5;extracted/Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)/_epub-src/OEBPS/ch01.html;u046-ch01-067)。
- 结论依据:原文在“Cell<T>”附近的定义、例子或代码支撑本卡核心说明:Cell通过整体值操作提供内部可变性,避免交出可随意别名的内部引用。边界按已读前提解释;示例未执行。
- 原始内容:Cell<T>, relaxes the rule (Item 15) that an item can be mutated only by its owner or by code that holds the (only) mutable reference to the item. This interior mutability allows for greater flexibility—for example, allowing trait implementations that mutate internals even when the method signature allows only &self. However, it also incurs costs: as well as