Skip to content

Commit 7f39a45

Browse files
Mention std::ptr::eq() as the equivalence to reference equality in Rust
1 parent 4b5728d commit 7f39a45

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/language/equality.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,21 @@ fn main() {
7070
}
7171
```
7272

73+
There is no direct equivalence to reference equality in Rust, since not everything is a reference.
74+
However, when you have two references, you can check for their reference equality with `std::ptr::eq()`
75+
76+
```rust
77+
fn main() {
78+
let a = 1;
79+
let b = 1;
80+
println!("{}", a == b); // true
81+
println!("{}", std::ptr::eq(&a, &b)); // false
82+
println!("{}", std::ptr::eq(&a, &a)); // true
83+
}
84+
```
85+
86+
Another way to compare for reference equality is to convert the references to raw pointers and compare them using `==`.
87+
7388
See also:
7489

7590
- [`Eq`][eq.rs] for a stricter version of `PartialEq`.

0 commit comments

Comments
 (0)