知识卡片
动态借用仍遵守独占规则
RefCell允许经共享引用申请内部借用,把多读或独占写的检查移到运行时,而不是取消规则。borrow/borrow_mut冲突时panic,try_borrow系列则返回可处理的错误。返回的Ref或RefMut守卫维持借用期间的限制,需释放守卫后再取得冲突访问。
这张卡片看不懂?
这张卡片的反馈已满 10 条,暂时无法继续提交。
已有反馈 · 0 / 10 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Chapter 1. Types Smart Pointer Types · 查看原书依据
RefCell<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
参考来源
- 位置:《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-066)。
- 结论依据:原文在“RefCell<T>”附近的定义、例子或代码支撑本卡核心说明:RefCell允许经共享引用申请内部借用,把多读或独占写的检查移到运行时,而不是取消规则。边界按已读前提解释;示例未执行。
- 原始内容:RefCell<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