知识卡片
位置索引会随集合变动失效
用姓名映射Vec下标可让Vec拥有唯一Guest,但删除前部元素会移动后续位置,旧下标可能指向另一客人或越界。整数仍合法不等于身份仍正确。所有改变位置的操作都需同步索引,外部长期保留的下标也需有效性设计。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Chapter 3. Concepts Data structure design · 查看原书依据
indexes into it are effectively acting like pointers, and we’ve reintroduced a world where a bug can lead those “pointers” to point to nothing (beyond the Vec bounds) or to point to incorrect data: ledger.register(alice); ledger.register(bob); ledger.register(charlie); println!("Register starts as: {ledger:?}"); ledger.deregister(0).unwrap(); println!("Regis
参考来源
- 位置:《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-019)。
- 结论依据:原文在“indexes into it are effectively acting like pointers”附近的定义、例子或代码支撑本卡核心说明:用姓名映射Vec下标可让Vec拥有唯一Guest,但删除前部元素会移动后续位置,旧下标可能指向另一客人或越界。边界按已读前提解释;示例未执行。
- 原始内容:indexes into it are effectively acting like pointers, and we’ve reintroduced a world where a bug can lead those “pointers” to point to nothing (beyond the Vec bounds) or to point to incorrect data: ledger.register(alice); ledger.register(bob); ledger.register(charlie); println!("Register starts as: {ledger:?}"); ledger.deregister(0).unwrap(); println!("Regis