]> git.lizzy.rs Git - rust.git/blob - src/docs/borrow_as_ptr.txt
Auto merge of #9421 - xphoniex:fix-#9420, r=giraffate
[rust.git] / src / docs / borrow_as_ptr.txt
1 ### What it does
2 Checks for the usage of `&expr as *const T` or
3 `&mut expr as *mut T`, and suggest using `ptr::addr_of` or
4 `ptr::addr_of_mut` instead.
5
6 ### Why is this bad?
7 This would improve readability and avoid creating a reference
8 that points to an uninitialized value or unaligned place.
9 Read the `ptr::addr_of` docs for more information.
10
11 ### Example
12 ```
13 let val = 1;
14 let p = &val as *const i32;
15
16 let mut val_mut = 1;
17 let p_mut = &mut val_mut as *mut i32;
18 ```
19 Use instead:
20 ```
21 let val = 1;
22 let p = std::ptr::addr_of!(val);
23
24 let mut val_mut = 1;
25 let p_mut = std::ptr::addr_of_mut!(val_mut);
26 ```