@@ -84,7 +84,7 @@ pub use crate::intrinsics::transmute;
84
84
///
85
85
/// let mut v = vec![65, 122];
86
86
/// // Build a `String` using the contents of `v`
87
- /// let s = unsafe { String::from_raw_parts(v.as_mut_ptr(), 2 , v.capacity()) };
87
+ /// let s = unsafe { String::from_raw_parts(v.as_mut_ptr(), v.len() , v.capacity()) };
88
88
/// // leak `v` because its memory is now managed by `s`
89
89
/// mem::forget(v); // ERROR - v is invalid and must not be passed to a function
90
90
/// assert_eq!(s, "Az");
@@ -113,10 +113,9 @@ pub use crate::intrinsics::transmute;
113
113
/// // does not get dropped!
114
114
/// let mut v = ManuallyDrop::new(v);
115
115
/// // Now disassemble `v`. These operations cannot panic, so there cannot be a leak.
116
- /// let ptr = v.as_mut_ptr();
117
- /// let cap = v.capacity();
116
+ /// let (ptr, len, cap) = (v.as_mut_ptr(), v.len(), v.capacity());
118
117
/// // Finally, build a `String`.
119
- /// let s = unsafe { String::from_raw_parts(ptr, 2 , cap) };
118
+ /// let s = unsafe { String::from_raw_parts(ptr, len , cap) };
120
119
/// assert_eq!(s, "Az");
121
120
/// // `s` is implicitly dropped and its memory deallocated.
122
121
/// ```
0 commit comments