about summary refs log tree commit diff
path: root/src/liballoc
diff options
context:
space:
mode:
authorJake Goulding <jake.goulding@gmail.com>2019-10-22 15:40:22 -0400
committerJake Goulding <jake.goulding@gmail.com>2019-10-25 11:22:53 -0400
commitdce8fabc62ed393152c53c65c492c8f3ae324eda (patch)
tree213b7603a91b180da60b90055a792bde8bb2b382 /src/liballoc
parent0d21d257c9691983fd51e7d5d9ace4de8933114c (diff)
downloadrust-dce8fabc62ed393152c53c65c492c8f3ae324eda.tar.gz
rust-dce8fabc62ed393152c53c65c492c8f3ae324eda.zip
Use ManuallyDrop in examples for {Vec,String}::from_raw_parts
Diffstat (limited to 'src/liballoc')
-rw-r--r--src/liballoc/string.rs16
-rw-r--r--src/liballoc/vec.rs10
2 files changed, 14 insertions, 12 deletions
diff --git a/src/liballoc/string.rs b/src/liballoc/string.rs
index 108c91fba1f..1d0faeccecf 100644
--- a/src/liballoc/string.rs
+++ b/src/liballoc/string.rs
@@ -194,7 +194,10 @@ use crate::vec::Vec;
 /// ```
 /// use std::mem;
 ///
-/// let mut story = String::from("Once upon a time...");
+/// let story = String::from("Once upon a time...");
+///
+/// // Prevent automatically dropping the String's data
+/// let mut story = mem::ManuallyDrop::new(story);
 ///
 /// let ptr = story.as_mut_ptr();
 /// let len = story.len();
@@ -203,9 +206,6 @@ use crate::vec::Vec;
 /// // story has nineteen bytes
 /// assert_eq!(19, len);
 ///
-/// // Now that we have our parts, we throw the story away.
-/// mem::forget(story);
-///
 /// // We can re-build a String out of ptr, len, and capacity. This is all
 /// // unsafe because we are responsible for making sure the components are
 /// // valid:
@@ -676,13 +676,15 @@ impl String {
     /// use std::mem;
     ///
     /// unsafe {
-    ///     let mut s = String::from("hello");
+    ///     let s = String::from("hello");
+    ///
+    ///     // Prevent automatically dropping the String's data
+    ///     let mut s = mem::ManuallyDrop::new(s);
+    ///
     ///     let ptr = s.as_mut_ptr();
     ///     let len = s.len();
     ///     let capacity = s.capacity();
     ///
-    ///     mem::forget(s);
-    ///
     ///     let s = String::from_raw_parts(ptr, len, capacity);
     ///
     ///     assert_eq!(String::from("hello"), s);
diff --git a/src/liballoc/vec.rs b/src/liballoc/vec.rs
index 6350b189c5f..c8fb8971234 100644
--- a/src/liballoc/vec.rs
+++ b/src/liballoc/vec.rs
@@ -389,7 +389,11 @@ impl<T> Vec<T> {
     /// use std::ptr;
     /// use std::mem;
     ///
-    /// let mut v = vec![1, 2, 3];
+    /// let v = vec![1, 2, 3];
+    ///
+    /// // Prevent running `v`'s destructor so we are in complete control
+    /// // of the allocation.
+    /// let mut v = mem::ManuallyDrop::new(v);
     ///
     /// // Pull out the various important pieces of information about `v`
     /// let p = v.as_mut_ptr();
@@ -397,10 +401,6 @@ impl<T> Vec<T> {
     /// let cap = v.capacity();
     ///
     /// unsafe {
-    ///     // Cast `v` into the void: no destructor run, so we are in
-    ///     // complete control of the allocation to which `p` points.
-    ///     mem::forget(v);
-    ///
     ///     // Overwrite memory with 4, 5, 6
     ///     for i in 0..len as isize {
     ///         ptr::write(p.offset(i), 4 + i);