From 82444aa753180c9c13028066ae9ddc4933dc610d Mon Sep 17 00:00:00 2001 From: mandeep Date: Fri, 5 Oct 2018 18:22:19 -0400 Subject: [PATCH 1/2] Add doc comments about safest way to initialize a vector of zeros --- src/liballoc/vec.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/liballoc/vec.rs b/src/liballoc/vec.rs index 2bc037e3fee12..3188de512662c 100644 --- a/src/liballoc/vec.rs +++ b/src/liballoc/vec.rs @@ -120,7 +120,9 @@ use raw_vec::RawVec; /// assert_eq!(vec, [1, 2, 3, 4]); /// ``` /// -/// It can also initialize each element of a `Vec` with a given value: +/// It can also initialize each element of a `Vec` with a given value. +/// Initializing a `Vec` in this manner is the most efficient and safest way to allocate a +/// vector of zeros as previously zeroed memory is requested from the operating system: /// /// ``` /// let vec = vec![0; 5]; From 1e584bf5c9858bee54a9fbff25ab28b2ad29eb57 Mon Sep 17 00:00:00 2001 From: mandeep Date: Tue, 9 Oct 2018 01:51:22 -0400 Subject: [PATCH 2/2] Refactor macro comment and add resize with zeros example --- src/liballoc/vec.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/liballoc/vec.rs b/src/liballoc/vec.rs index 3188de512662c..f7a0bbdceafc9 100644 --- a/src/liballoc/vec.rs +++ b/src/liballoc/vec.rs @@ -121,12 +121,16 @@ use raw_vec::RawVec; /// ``` /// /// It can also initialize each element of a `Vec` with a given value. -/// Initializing a `Vec` in this manner is the most efficient and safest way to allocate a -/// vector of zeros as previously zeroed memory is requested from the operating system: +/// 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` as an efficient stack: