Skip to content

Commit fd6d480

Browse files
authored
Add speaker note about reference equality (#2670)
1 parent 5ab6fae commit fd6d480

File tree

1 file changed

+28
-14
lines changed

1 file changed

+28
-14
lines changed

src/std-traits/comparisons.md

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,35 @@ impl PartialOrd for Citation {
5454

5555
<details>
5656

57-
`PartialEq` can be implemented between different types, but `Eq` cannot, because
58-
it is reflexive:
57+
- `PartialEq` can be implemented between different types, but `Eq` cannot,
58+
because it is reflexive:
5959

60-
```rust,editable
61-
struct Key {
62-
id: u32,
63-
metadata: Option<String>,
64-
}
65-
impl PartialEq<u32> for Key {
66-
fn eq(&self, other: &u32) -> bool {
67-
self.id == *other
68-
}
69-
}
70-
```
60+
```rust,editable
61+
struct Key {
62+
id: u32,
63+
metadata: Option<String>,
64+
}
65+
impl PartialEq<u32> for Key {
66+
fn eq(&self, other: &u32) -> bool {
67+
self.id == *other
68+
}
69+
}
70+
```
71+
72+
- In practice, it's common to derive these traits, but uncommon to implement
73+
them.
74+
75+
- When comparing references in Rust, it will will compare the value of the
76+
things pointed to, it will NOT compare the references themselves. That means
77+
that references to two different things can compare as equal if the values
78+
pointed to are the same:
7179

72-
In practice, it's common to derive these traits, but uncommon to implement them.
80+
```rust,editable
81+
fn main() {
82+
let a = "Hello";
83+
let b = String::from("Hello");
84+
assert_eq!(a, b);
85+
}
86+
```
7387

7488
</details>

0 commit comments

Comments
 (0)