知识卡片
可选引用利用空地址
安全 Rust 的普通引用 &T 总有有效的所指值。可能查不到对象时,用 Option<&T> 返回 Some(r) 或 None,使用者先区分这两种状态,再取得引用。瘦引用的地址零本来就不用于有效引用,因此可用它表示 None,同时保持与同类引用相同的大小。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- References 用类型表达引用可能缺席 · 查看原书依据
Rust represents None as a null pointer and Some(r), where r is a &T value, as the nonzero address,
参考来源
- 位置:《Programming Rust Fast, Safe Systems Development, 3rd ed. (First Early Release)》第04章《References》|用类型表达引用可能缺席,来源项目 `book-source`,`extracted/Programming Rust Fast, Safe Systems Development, 3rd ed. (First Early Release)/_epub-src/OEBPS/ch04.xhtml#references-are-never-null`
- 结论依据:原文用空指针表示缺席作对照,给出 None 与 Some 的不同底层表示,并说明类型强制处理缺席状态。
- 原始内容:Rust represents None as a null pointer and Some(r), where r is a &T value, as the nonzero address,