]> git.lizzy.rs Git - rust.git/blob - src/docs/ptr_eq.txt
Auto merge of #9425 - kraktus:patch-1, r=xFrednet
[rust.git] / src / docs / ptr_eq.txt
1 ### What it does
2 Use `std::ptr::eq` when applicable
3
4 ### Why is this bad?
5 `ptr::eq` can be used to compare `&T` references
6 (which coerce to `*const T` implicitly) by their address rather than
7 comparing the values they point to.
8
9 ### Example
10 ```
11 let a = &[1, 2, 3];
12 let b = &[1, 2, 3];
13
14 assert!(a as *const _ as usize == b as *const _ as usize);
15 ```
16 Use instead:
17 ```
18 let a = &[1, 2, 3];
19 let b = &[1, 2, 3];
20
21 assert!(std::ptr::eq(a, b));
22 ```