知识卡片

普通读书笔记卡 · 6074.e.1.g · 生成模型:未记录

借用守卫界定排他范围

理解等级:中级

borrow() 返回 Ref,borrow_mut() 返回 RefMut。守卫活着时容器记录相应借用。守卫释放后该次借用结束。同一 RefCell 的两个 RefMut 若重叠,第二次取得守卫时会 panic。缩小守卫持有区间可避免无意中重叠。

相关卡片 [[内部可变性延后借用检查]]

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 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.