@@ -194,7 +194,10 @@ use crate::vec::Vec;
194194/// ```
195195/// use std::mem;
196196///
197- /// let mut story = String::from("Once upon a time...");
197+ /// let story = String::from("Once upon a time...");
198+ ///
199+ /// // Prevent automatically dropping the String's data
200+ /// let mut story = mem::ManuallyDrop::new(story);
198201///
199202/// let ptr = story.as_mut_ptr();
200203/// let len = story.len();
@@ -203,9 +206,6 @@ use crate::vec::Vec;
203206/// // story has nineteen bytes
204207/// assert_eq!(19, len);
205208///
206- /// // Now that we have our parts, we throw the story away.
207- /// mem::forget(story);
208- ///
209209/// // We can re-build a String out of ptr, len, and capacity. This is all
210210/// // unsafe because we are responsible for making sure the components are
211211/// // valid:
@@ -676,13 +676,15 @@ impl String {
676676 /// use std::mem;
677677 ///
678678 /// unsafe {
679- /// let mut s = String::from("hello");
679+ /// let s = String::from("hello");
680+ ///
681+ /// // Prevent automatically dropping the String's data
682+ /// let mut s = mem::ManuallyDrop::new(s);
683+ ///
680684 /// let ptr = s.as_mut_ptr();
681685 /// let len = s.len();
682686 /// let capacity = s.capacity();
683687 ///
684- /// mem::forget(s);
685- ///
686688 /// let s = String::from_raw_parts(ptr, len, capacity);
687689 ///
688690 /// assert_eq!(String::from("hello"), s);
0 commit comments