From be8d728f3cb1cb79a630c6e6aba6df923dd3e999 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sun, 24 Feb 2019 16:58:04 +0100 Subject: [PATCH] show how to set with ptr::write --- src/libcore/mem.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/libcore/mem.rs b/src/libcore/mem.rs index d4d51f8eeb76e..967a36f6f1c39 100644 --- a/src/libcore/mem.rs +++ b/src/libcore/mem.rs @@ -1191,7 +1191,8 @@ impl MaybeUninit { } /// Sets the value of the `MaybeUninit`. This overwrites any previous value - /// without dropping it. For your convenience, this also returns a mutable + /// without dropping it, so be careful not to use this twice unless you want to + /// skip running the destructor. For your convenience, this also returns a mutable /// reference to the (now safely initialized) contents of `self`. #[unstable(feature = "maybe_uninit", issue = "53491")] #[inline(always)] @@ -1214,7 +1215,7 @@ impl MaybeUninit { /// use std::mem::MaybeUninit; /// /// let mut x = MaybeUninit::>::uninitialized(); - /// x.set(vec![0,1,2]); + /// unsafe { x.as_mut_ptr().write(vec![0,1,2]); } /// // Create a reference into the `MaybeUninit`. This is okay because we initialized it. /// let x_vec = unsafe { &*x.as_ptr() }; /// assert_eq!(x_vec.len(), 3); @@ -1250,7 +1251,7 @@ impl MaybeUninit { /// use std::mem::MaybeUninit; /// /// let mut x = MaybeUninit::>::uninitialized(); - /// x.set(vec![0,1,2]); + /// unsafe { x.as_mut_ptr().write(vec![0,1,2]); } /// // Create a reference into the `MaybeUninit>`. /// // This is okay because we initialized it. /// let x_vec = unsafe { &mut *x.as_mut_ptr() }; @@ -1295,7 +1296,7 @@ impl MaybeUninit { /// use std::mem::MaybeUninit; /// /// let mut x = MaybeUninit::::uninitialized(); - /// x.set(true); + /// unsafe { x.as_mut_ptr().write(true); } /// let x_init = unsafe { x.into_initialized() }; /// assert_eq!(x_init, true); /// ```