]> git.lizzy.rs Git - rust.git/commitdiff
Rewrite/expand doc examples for `Vec::set_len`.
authorCorey Farwell <coreyf@rwell.org>
Tue, 19 Jul 2016 02:29:05 +0000 (22:29 -0400)
committerCorey Farwell <coreyf@rwell.org>
Tue, 19 Jul 2016 16:31:41 +0000 (12:31 -0400)
src/libcollections/vec.rs

index da56b21cf0c050c8a2c0bd3a1282e583e47f789f..9badf8cf1830584abb2b8e405c0beac8850853f6 100644 (file)
@@ -532,9 +532,37 @@ pub fn as_mut_slice(&mut self) -> &mut [T] {
     /// # Examples
     ///
     /// ```
-    /// let mut v = vec![1, 2, 3, 4];
+    /// use std::ptr;
+    ///
+    /// let mut vec = vec!['r', 'u', 's', 't'];
+    ///
+    /// unsafe {
+    ///     ptr::drop_in_place(&mut vec[3]);
+    ///     vec.set_len(3);
+    /// }
+    /// assert_eq!(vec, ['r', 'u', 's']);
+    /// ```
+    ///
+    /// In this example, there is a memory leak since the memory locations
+    /// owned by the vector were not freed prior to the `set_len` call:
+    ///
+    /// ```
+    /// let mut vec = vec!['r', 'u', 's', 't'];
+    ///
+    /// unsafe {
+    ///     vec.set_len(0);
+    /// }
+    /// ```
+    ///
+    /// In this example, the vector gets expanded from zero to four items
+    /// without any memory allocations occurring, resulting in vector
+    /// values of unallocated memory:
+    ///
+    /// ```
+    /// let mut vec: Vec<char> = Vec::new();
+    ///
     /// unsafe {
-    ///     v.set_len(1);
+    ///     vec.set_len(4);
     /// }
     /// ```
     #[inline]