知识卡片
引用比较值或地址
对同层的整数引用使用 ==,会沿每一层引用比较最终的整数。x、y 各自存有 10 时,&x == &y 成立,但 std::ptr::eq(&x, &y) 检查的是它们是否指向同一个位置。比较整数引用时需要对齐引用层数,如用 rx == *rrx 比较 &i32 与解开一层的 &&i32。查内容一致性选值比较,查身份选指针比较。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- References 按值相等与指向同一地址 · 查看原书依据
If you actually want to know whether two references point to the same memory, you can use std::ptr::eq, which compares them as addresses:
参考来源
- 位置:《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#comparing-references`
- 结论依据:同值异址例子对照了普通比较与 std::ptr::eq,随后使用单层与双层引用演示类型不匹配。
- 原始内容:If you actually want to know whether two references point to the same memory, you can use std::ptr::eq, which compares them as addresses: