知识卡片
可选引用显式表示缺席
普通引用 &T 保证非空。需要表达“可能没有被借用的值”时,Option<&T> 用 Some(r) 包装有效引用,用 None 表达缺席。借用引用留出来的空地址表示可用于 None,因此这个可选类型与 &T 大小相同。
关联阅读:[[指针类型决定释放责任]]。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Fundamental Types 指针类型说明谁管理内存 · 查看原书依据
Use Option for optional pointers, with None representing null. There’s no extra memory cost; an Option<&T> is the same size as a &T.
参考来源
- 位置:《Programming Rust Fast, Safe Systems Development, 3rd ed. (First Early Release)》第02章《Fundamental Types》|指针类型说明谁管理内存,来源项目 `book-source`,`extracted/Programming Rust Fast, Safe Systems Development, 3rd ed. (First Early Release)/_epub-src/OEBPS/ch02.xhtml#pointer-types`
- 结论依据:原文说明非裸指针的非空约束、Option 的缺席语义,并明确给出共享引用的大小比较。
- 原始内容:Use Option for optional pointers, with None representing null. There’s no extra memory cost; an Option<&T> is the same size as a &T.