about summary refs log tree commit diff
path: root/src/liballoc
diff options
context:
space:
mode:
authorkennytm <kennytm@gmail.com>2018-10-12 22:04:04 +0800
committerGitHub <noreply@github.com>2018-10-12 22:04:04 +0800
commit365d4c94f6eca04c03203c005c5711eee30d5ff6 (patch)
treee8735b7574efe95529cff75b7a176b7daa29a5ee /src/liballoc
parent44a527a27a7fdeb14a208ca195a62b58df4b960d (diff)
parent1e584bf5c9858bee54a9fbff25ab28b2ad29eb57 (diff)
downloadrust-365d4c94f6eca04c03203c005c5711eee30d5ff6.tar.gz
rust-365d4c94f6eca04c03203c005c5711eee30d5ff6.zip
Rollup merge of #54860 - mandeep:vec-initialize, r=alexcrichton
Add doc comments about safest way to initialize a vector of zeros

This adds more information about the vec! macro as discussed in #54628. I think this is a good starting point, but I think additional detail is needed so that we can explain why vec! is safer than the alternatives.
Diffstat (limited to 'src/liballoc')
-rw-r--r--src/liballoc/vec.rs8
1 files changed, 7 insertions, 1 deletions
diff --git a/src/liballoc/vec.rs b/src/liballoc/vec.rs
index 2bc037e3fee..f7a0bbdceaf 100644
--- a/src/liballoc/vec.rs
+++ b/src/liballoc/vec.rs
@@ -120,11 +120,17 @@ use raw_vec::RawVec;
 /// assert_eq!(vec, [1, 2, 3, 4]);
 /// ```
 ///
-/// It can also initialize each element of a `Vec<T>` with a given value:
+/// It can also initialize each element of a `Vec<T>` with a given value.
+/// This may be more efficient than performing allocation and initialization
+/// in separate steps, especially when initializing a vector of zeros:
 ///
 /// ```
 /// let vec = vec![0; 5];
 /// assert_eq!(vec, [0, 0, 0, 0, 0]);
+///
+/// // The following is equivalent, but potentially slower:
+/// let mut vec1 = Vec::with_capacity(5);
+/// vec1.resize(5, 0);
 /// ```
 ///
 /// Use a `Vec<T>` as an efficient stack: