知识卡片
自引用需要额外不变量
对象同时拥有数据并保存内部引用时,要保证初始化、移动、修改和析构都不破坏引用。给结构体加外部寿命参数不能自动证明这种关系。对String的切片还需防止缓冲重分配;移动String句柄本身通常不移动其堆内容,不能把所有风险只归因于外层地址变化。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Chapter 3. Concepts Self-referential data structures · 查看原书依据
Self-referential data structures One particular battle with the borrow checker always stymies programmers arriving at Rust from other languages: attempting to create self-referential data structures, which contain a mixture of owned data together with references to within that owned data: struct SelfRef { text: String, // The slice of `text` that holds the t
参考来源
- 位置:《Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)》第3章《Self-referential data structures》(spine7;extracted/Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)/_epub-src/OEBPS/ch03.html;u096-ch03-023)。
- 结论依据:原文在“Self-referential data structures”附近的定义、例子或代码支撑本卡核心说明:对象同时拥有数据并保存内部引用时,要保证初始化、移动、修改和析构都不破坏引用。边界按已读前提解释;示例未执行。
- 原始内容:Self-referential data structures One particular battle with the borrow checker always stymies programmers arriving at Rust from other languages: attempting to create self-referential data structures, which contain a mixture of owned data together with references to within that owned data: struct SelfRef { text: String, // The slice of `text` that holds the t