about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRalf Jung <post@ralfj.de>2019-02-22 22:11:37 +0100
committerRalf Jung <post@ralfj.de>2019-02-22 22:37:54 +0100
commitdc570fb3f26b45d101440bb4fe57121ca06884d3 (patch)
tree11a206ed1f9a6ade7d738737fa51f73bdd9f0a60
parent13ffbee1735c7329d6e7b3521712996c914c4ef9 (diff)
downloadrust-dc570fb3f26b45d101440bb4fe57121ca06884d3.tar.gz
rust-dc570fb3f26b45d101440bb4fe57121ca06884d3.zip
also add examples to MaybeUninit::into_initialized
-rw-r--r--src/libcore/mem.rs29
1 files changed, 28 insertions, 1 deletions
diff --git a/src/libcore/mem.rs b/src/libcore/mem.rs
index 3955839ac36..42f30c1dd6d 100644
--- a/src/libcore/mem.rs
+++ b/src/libcore/mem.rs
@@ -1178,6 +1178,31 @@ impl<T> MaybeUninit<T> {
     /// It is up to the caller to guarantee that the `MaybeUninit` really is in an initialized
     /// state. Calling this when the content is not yet fully initialized causes undefined
     /// behavior.
+    ///
+    /// # Examples
+    ///
+    /// Correct usage of this method:
+    ///
+    /// ```rust
+    /// #![feature(maybe_uninit)]
+    /// use std::mem::MaybeUninit;
+    ///
+    /// let mut x = MaybeUninit::<bool>::uninitialized();
+    /// x.set(true);
+    /// let x_init = unsafe { x.into_initialized() };
+    /// assert_eq!(x_init, true);
+    /// ```
+    ///
+    /// *Incorrect* usage of this method:
+    ///
+    /// ```rust,no_run
+    /// #![feature(maybe_uninit)]
+    /// use std::mem::MaybeUninit;
+    ///
+    /// let x = MaybeUninit::<Vec<u32>>::uninitialized();
+    /// let x_init = unsafe { x.into_initialized() };
+    /// // `x` had not been initialized yet, so this last line causes undefined behavior.
+    /// ```
     #[unstable(feature = "maybe_uninit", issue = "53491")]
     #[inline(always)]
     pub unsafe fn into_initialized(self) -> T {
@@ -1212,15 +1237,17 @@ impl<T> MaybeUninit<T> {
     /// let x1 = unsafe { x.read_initialized() };
     /// // `u32` is `Copy`, so we may read multiple times.
     /// let x2 = unsafe { x.read_initialized() };
+    /// assert_eq!(x1, x2);
     ///
     /// let mut x = MaybeUninit::<Option<Vec<u32>>>::uninitialized();
     /// x.set(None);
     /// let x1 = unsafe { x.read_initialized() };
     /// // Duplicating a `None` value is okay, so we may read multiple times.
     /// let x2 = unsafe { x.read_initialized() };
+    /// assert_eq!(x1, x2);
     /// ```
     ///
-    /// *Incorrect* usafe of this method:
+    /// *Incorrect* usage of this method:
     ///
     /// ```rust,no_run
     /// #![feature(maybe_uninit)]