知识卡片
共享可变值的两层职责
Rc<RefCell<T>> 让多个句柄共享一个 RefCell。使用者通过 borrow_mut() 临时取得独占访问,所有其他句柄后来看到同一份更新。强计数管理寿命,借用守卫管理谁此刻访问,动态排他性依旧适用。
相关卡片 [[Rc 共享同一份所有权]]、[[借用守卫界定排他范围]]
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- 智能指针 Allowing Multiple Owners of Mutable Data with Rc<T> and RefCell<T> · 查看原书依据
A common way to use RefCell<T> is in combination with Rc<T>. Recall that Rc<T> lets you have multiple owners of some data, but it only gives immutable access to that data. If you have an Rc<T> that holds a RefCell<T>, you can get a value that can have multiple owners and that you can mutate!
参考来源
- 位置:《The Rust Programming Language(第2版)》第15章 智能指针(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c15.xhtml`,块 0282)
- 结论依据:原文指出 Rc 与 RefCell 分别解决共享所有权与内容可变。
- 原始内容:A common way to use RefCell<T> is in combination with Rc<T>. Recall that Rc<T> lets you have multiple owners of some data, but it only gives immutable access to that data. If you have an Rc<T> that holds a RefCell<T>, you can get a value that can have multiple owners and that you can mutate!