about summary refs log tree commit diff
path: root/src/libcore
diff options
context:
space:
mode:
authorHrvoje Nikšić <hniksic@gmail.com>2020-03-19 13:56:48 +0100
committerHrvoje Niksic <hniksic@gmail.com>2020-03-19 14:50:33 +0100
commit2bebe8d87111b544b8f5600fe93cc96391d5c91e (patch)
tree75509cb9e5f4a132bc66ace8dbcf7f8c97c7e3f3 /src/libcore
parent755434121cda7d21d301592cf6fbddb7042e59a9 (diff)
downloadrust-2bebe8d87111b544b8f5600fe93cc96391d5c91e.tar.gz
rust-2bebe8d87111b544b8f5600fe93cc96391d5c91e.zip
Don't hard-code the vector length in the examples.
Co-Authored-By: lzutao <taolzu@gmail.com>
Diffstat (limited to 'src/libcore')
-rw-r--r--src/libcore/mem/mod.rs7
1 files changed, 3 insertions, 4 deletions
diff --git a/src/libcore/mem/mod.rs b/src/libcore/mem/mod.rs
index 253847612ad..dac9ee6a5d9 100644
--- a/src/libcore/mem/mod.rs
+++ b/src/libcore/mem/mod.rs
@@ -84,7 +84,7 @@ pub use crate::intrinsics::transmute;
 ///
 /// let mut v = vec![65, 122];
 /// // Build a `String` using the contents of `v`
-/// let s = unsafe { String::from_raw_parts(v.as_mut_ptr(), 2, v.capacity()) };
+/// let s = unsafe { String::from_raw_parts(v.as_mut_ptr(), v.len(), v.capacity()) };
 /// // leak `v` because its memory is now managed by `s`
 /// mem::forget(v);  // ERROR - v is invalid and must not be passed to a function
 /// assert_eq!(s, "Az");
@@ -113,10 +113,9 @@ pub use crate::intrinsics::transmute;
 /// // does not get dropped!
 /// let mut v = ManuallyDrop::new(v);
 /// // Now disassemble `v`. These operations cannot panic, so there cannot be a leak.
-/// let ptr = v.as_mut_ptr();
-/// let cap = v.capacity();
+/// let (ptr, len, cap) = (v.as_mut_ptr(), v.len(), v.capacity());
 /// // Finally, build a `String`.
-/// let s = unsafe { String::from_raw_parts(ptr, 2, cap) };
+/// let s = unsafe { String::from_raw_parts(ptr, len, cap) };
 /// assert_eq!(s, "Az");
 /// // `s` is implicitly dropped and its memory deallocated.
 /// ```