@@ -194,7 +194,10 @@ use crate::vec::Vec;
194
194
/// ```
195
195
/// use std::mem;
196
196
///
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);
198
201
///
199
202
/// let ptr = story.as_mut_ptr();
200
203
/// let len = story.len();
@@ -203,9 +206,6 @@ use crate::vec::Vec;
203
206
/// // story has nineteen bytes
204
207
/// assert_eq!(19, len);
205
208
///
206
- /// // Now that we have our parts, we throw the story away.
207
- /// mem::forget(story);
208
- ///
209
209
/// // We can re-build a String out of ptr, len, and capacity. This is all
210
210
/// // unsafe because we are responsible for making sure the components are
211
211
/// // valid:
@@ -676,13 +676,15 @@ impl String {
676
676
/// use std::mem;
677
677
///
678
678
/// 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
+ ///
680
684
/// let ptr = s.as_mut_ptr();
681
685
/// let len = s.len();
682
686
/// let capacity = s.capacity();
683
687
///
684
- /// mem::forget(s);
685
- ///
686
688
/// let s = String::from_raw_parts(ptr, len, capacity);
687
689
///
688
690
/// assert_eq!(String::from("hello"), s);
0 commit comments