Skip to content

Commit 2bebe8d

Browse files
hniksictesuji
andcommitted
Don't hard-code the vector length in the examples.
Co-Authored-By: lzutao <taolzu@gmail.com>
1 parent 7554341 commit 2bebe8d

File tree

1 file changed

+3
-4
lines changed

1 file changed

+3
-4
lines changed

src/libcore/mem/mod.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ pub use crate::intrinsics::transmute;
8484
///
8585
/// let mut v = vec![65, 122];
8686
/// // 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()) };
8888
/// // leak `v` because its memory is now managed by `s`
8989
/// mem::forget(v); // ERROR - v is invalid and must not be passed to a function
9090
/// assert_eq!(s, "Az");
@@ -113,10 +113,9 @@ pub use crate::intrinsics::transmute;
113113
/// // does not get dropped!
114114
/// let mut v = ManuallyDrop::new(v);
115115
/// // 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());
118117
/// // 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) };
120119
/// assert_eq!(s, "Az");
121120
/// // `s` is implicitly dropped and its memory deallocated.
122121
/// ```

0 commit comments

Comments
 (0)