Skip to content

Commit d2c1d5c

Browse files
authored
Merge pull request #476 from nikthechampiongr/raw_ref
Update guidance on uninitialized fields to use &raw mut instead of addr_of_mut!
2 parents 625b200 + 470f2bf commit d2c1d5c

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

src/unchecked-uninit.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ to compute the address of array index `idx`. This relies on
134134
how arrays are laid out in memory.
135135
* For a struct, however, in general we do not know how it is laid out, and we
136136
also cannot use `&mut base_ptr.field` as that would be creating a
137-
reference. So, you must carefully use the [`addr_of_mut`] macro. This creates
137+
reference. So, you must carefully use the [raw reference][raw_reference] syntax. This creates
138138
a raw pointer to the field without creating an intermediate reference:
139139

140140
```rust
@@ -147,7 +147,7 @@ struct Demo {
147147
let mut uninit = MaybeUninit::<Demo>::uninit();
148148
// `&uninit.as_mut().field` would create a reference to an uninitialized `bool`,
149149
// and thus be Undefined Behavior!
150-
let f1_ptr = unsafe { ptr::addr_of_mut!((*uninit.as_mut_ptr()).field) };
150+
let f1_ptr = unsafe { &raw mut (*uninit.as_mut_ptr()).field };
151151
unsafe { f1_ptr.write(true); }
152152

153153
let init = unsafe { uninit.assume_init() };
@@ -167,7 +167,7 @@ it around at all, be sure to be *really* careful.
167167
[`MaybeUninit`]: ../core/mem/union.MaybeUninit.html
168168
[assume_init]: ../core/mem/union.MaybeUninit.html#method.assume_init
169169
[`ptr`]: ../core/ptr/index.html
170-
[`addr_of_mut`]: ../core/ptr/macro.addr_of_mut.html
170+
[raw_reference]: ../reference/types/pointer.html#r-type.pointer.raw.constructor
171171
[`write`]: ../core/ptr/fn.write.html
172172
[`copy`]: ../std/ptr/fn.copy.html
173173
[`copy_nonoverlapping`]: ../std/ptr/fn.copy_nonoverlapping.html

0 commit comments

Comments
 (0)