about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorCorey Farwell <coreyf@rwell.org>2016-07-18 22:29:05 -0400
committerCorey Farwell <coreyf@rwell.org>2016-07-19 12:31:41 -0400
commita005b2cd2ac679da7393e537aa05e2b7d32d36d5 (patch)
tree87caf477adefed21c712003051059bba3b2ebbc3 /src
parent6aba7be9a67467d31e6cbf75dc8b5f44d60cb5ca (diff)
downloadrust-a005b2cd2ac679da7393e537aa05e2b7d32d36d5.tar.gz
rust-a005b2cd2ac679da7393e537aa05e2b7d32d36d5.zip
Rewrite/expand doc examples for `Vec::set_len`.
Diffstat (limited to 'src')
-rw-r--r--src/libcollections/vec.rs32
1 files changed, 30 insertions, 2 deletions
diff --git a/src/libcollections/vec.rs b/src/libcollections/vec.rs
index da56b21cf0c..9badf8cf183 100644
--- a/src/libcollections/vec.rs
+++ b/src/libcollections/vec.rs
@@ -532,9 +532,37 @@ impl<T> Vec<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]