Skip to content

Commit be8d728

Browse files
committed
show how to set with ptr::write
1 parent a5e2d0c commit be8d728

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/libcore/mem.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1191,7 +1191,8 @@ impl<T> MaybeUninit<T> {
11911191
}
11921192

11931193
/// Sets the value of the `MaybeUninit<T>`. This overwrites any previous value
1194-
/// without dropping it. For your convenience, this also returns a mutable
1194+
/// without dropping it, so be careful not to use this twice unless you want to
1195+
/// skip running the destructor. For your convenience, this also returns a mutable
11951196
/// reference to the (now safely initialized) contents of `self`.
11961197
#[unstable(feature = "maybe_uninit", issue = "53491")]
11971198
#[inline(always)]
@@ -1214,7 +1215,7 @@ impl<T> MaybeUninit<T> {
12141215
/// use std::mem::MaybeUninit;
12151216
///
12161217
/// let mut x = MaybeUninit::<Vec<u32>>::uninitialized();
1217-
/// x.set(vec![0,1,2]);
1218+
/// unsafe { x.as_mut_ptr().write(vec![0,1,2]); }
12181219
/// // Create a reference into the `MaybeUninit<T>`. This is okay because we initialized it.
12191220
/// let x_vec = unsafe { &*x.as_ptr() };
12201221
/// assert_eq!(x_vec.len(), 3);
@@ -1250,7 +1251,7 @@ impl<T> MaybeUninit<T> {
12501251
/// use std::mem::MaybeUninit;
12511252
///
12521253
/// let mut x = MaybeUninit::<Vec<u32>>::uninitialized();
1253-
/// x.set(vec![0,1,2]);
1254+
/// unsafe { x.as_mut_ptr().write(vec![0,1,2]); }
12541255
/// // Create a reference into the `MaybeUninit<Vec<u32>>`.
12551256
/// // This is okay because we initialized it.
12561257
/// let x_vec = unsafe { &mut *x.as_mut_ptr() };
@@ -1295,7 +1296,7 @@ impl<T> MaybeUninit<T> {
12951296
/// use std::mem::MaybeUninit;
12961297
///
12971298
/// let mut x = MaybeUninit::<bool>::uninitialized();
1298-
/// x.set(true);
1299+
/// unsafe { x.as_mut_ptr().write(true); }
12991300
/// let x_init = unsafe { x.into_initialized() };
13001301
/// assert_eq!(x_init, true);
13011302
/// ```

0 commit comments

Comments
 (0)