mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-04-22 15:57:46 +02:00
Add speaker note about reference equality (#2670)
This commit is contained in:
parent
5ab6fae9e9
commit
fd6d4807a0
@ -54,21 +54,35 @@ impl PartialOrd for Citation {
|
|||||||
|
|
||||||
<details>
|
<details>
|
||||||
|
|
||||||
`PartialEq` can be implemented between different types, but `Eq` cannot, because
|
- `PartialEq` can be implemented between different types, but `Eq` cannot,
|
||||||
it is reflexive:
|
because it is reflexive:
|
||||||
|
|
||||||
```rust,editable
|
```rust,editable
|
||||||
struct Key {
|
struct Key {
|
||||||
id: u32,
|
id: u32,
|
||||||
metadata: Option<String>,
|
metadata: Option<String>,
|
||||||
}
|
}
|
||||||
impl PartialEq<u32> for Key {
|
impl PartialEq<u32> for Key {
|
||||||
fn eq(&self, other: &u32) -> bool {
|
fn eq(&self, other: &u32) -> bool {
|
||||||
self.id == *other
|
self.id == *other
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
In practice, it's common to derive these traits, but uncommon to implement them.
|
- In practice, it's common to derive these traits, but uncommon to implement
|
||||||
|
them.
|
||||||
|
|
||||||
|
- When comparing references in Rust, it will will compare the value of the
|
||||||
|
things pointed to, it will NOT compare the references themselves. That means
|
||||||
|
that references to two different things can compare as equal if the values
|
||||||
|
pointed to are the same:
|
||||||
|
|
||||||
|
```rust,editable
|
||||||
|
fn main() {
|
||||||
|
let a = "Hello";
|
||||||
|
let b = String::from("Hello");
|
||||||
|
assert_eq!(a, b);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
</details>
|
</details>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user