知识卡片
借用守卫界定排他范围
borrow() 返回 Ref,borrow_mut() 返回 RefMut。守卫活着时容器记录相应借用。守卫释放后该次借用结束。同一 RefCell 的两个 RefMut 若重叠,第二次取得守卫时会 panic。缩小守卫持有区间可避免无意中重叠。
相关卡片 [[内部可变性延后借用检查]]
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- 智能指针 A Use Case for Interior Mutability: Mock Objects · 查看原书依据
The RefCell<T> keeps track of how many Ref<T> and RefMut<T> smart pointers are currently active. Every time we call borrow, the RefCell<T> increases its count of how many immutable borrows are active. When a Ref<T> value goes out of scope, the count of immutable borrows goes down by 1.
参考来源
- 位置:《The Rust Programming Language(第2版)》第15章 智能指针(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c15.xhtml`,块 0272)
- 结论依据:原文解释 Ref 守卫何时计数增加和减少,并给出允许的借用组合。
- 原始内容:The RefCell<T> keeps track of how many Ref<T> and RefMut<T> smart pointers are currently active. Every time we call borrow, the RefCell<T> increases its count of how many immutable borrows are active. When a Ref<T> value goes out of scope, the count of immutable borrows goes down by 1.