知识卡片
共享对象身份不能代替索引一致性
两种视图保存同一Rc>,可避免Vec位置变动使姓名指向另一个对象,并让字段修改共享可见。但从Vec删除后,名称映射仍可能留有该客人,必须同步删除。若允许改名或重复名,还需维护键与对象身份约束;智能指针不会自动完成业务一致性。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Chapter 3. Concepts Data structure design · 查看原书依据
Rc and RefCell avoids the invalidation problems of using indices as pseudo-pointers. Updating the example—but keeping the bug in it—gives the following: mod rc { use super::{Error, Guest}; use std::{cell::RefCell, rc::Rc}; #[derive(Default)] pub struct GuestRegister { by_arrival: Vec<Rc<RefCell<Guest>>>, by_name: std::collections::BTreeMap<String, Rc<RefCell
参考来源
- 位置:《Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)》第3章《Data structure design》(spine7;extracted/Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)/_epub-src/OEBPS/ch03.html;u094-ch03-020)。
- 结论依据:原文在“Rc and RefCell”附近的定义、例子或代码支撑本卡核心说明:两种视图保存同一Rc<RefCell<Guest>>,可避免Vec位置变动使姓名指向另一个对象,并让字段修改共享可见。边界按已读前提解释;示例未执行。
- 原始内容:Rc and RefCell avoids the invalidation problems of using indices as pseudo-pointers. Updating the example—but keeping the bug in it—gives the following: mod rc { use super::{Error, Guest}; use std::{cell::RefCell, rc::Rc}; #[derive(Default)] pub struct GuestRegister { by_arrival: Vec<Rc<RefCell<Guest>>>, by_name: std::collections::BTreeMap<String, Rc<RefCell