]> git.lizzy.rs Git - rust.git/commitdiff
Rollup merge of #34911 - frewsxcv:vec-set-len, r=steveklabnik
authorGuillaume Gomez <guillaume1.gomez@gmail.com>
Thu, 21 Jul 2016 09:27:00 +0000 (11:27 +0200)
committerGitHub <noreply@github.com>
Thu, 21 Jul 2016 09:27:00 +0000 (11:27 +0200)
Rewrite/expand doc examples for `Vec::set_len`.

None

src/libcollections/vec.rs

index 518b94b5031b436f8f2372d86a2006fb83cc1af0..9ca3a5bd6079ad7a7c020a3520a323d466abf4c6 100644 (file)
@@ -565,9 +565,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]