@@ -20,6 +20,20 @@ declare_clippy_lint! {
2020 /// These structures are non-idiomatic and less efficient than simply using
2121 /// `vec![0; len]`.
2222 ///
23+ /// More specifically, for `vec![0; len]`, the compiler can use a more specialized type of allocation
24+ /// that also zero-initializes the allocated memory in the same call
25+ /// (see: [alloc_zeroed](https://doc.rust-lang.org/stable/std/alloc/trait.GlobalAlloc.html#method.alloc_zeroed)).
26+ ///
27+ /// Writing `Vec::new()` followed by `vec.resize(len, 0)` is suboptimal because,
28+ /// while it does do the same number of allocations,
29+ /// it involves two operations for allocating and initializing.
30+ /// The `resize` call first allocates memory (since `Vec::new()` did not), and only *then* zero-initializes it.
31+ ///
32+ /// Writing `Vec::with_capacity(size)` followed by `vec.resize(len, 0)` is similar.
33+ /// The allocation shifts from `resize` to `with_capacity`,
34+ /// but the zero-initialization still happens separately,
35+ /// when it could be done in one call with `vec![0; len]` (`alloc_zeroed`).
36+ ///
2337 /// ### Example
2438 /// ```rust
2539 /// # use core::iter::repeat;
@@ -32,13 +46,17 @@ declare_clippy_lint! {
3246 ///
3347 /// let mut vec2 = Vec::with_capacity(len);
3448 /// vec2.extend(repeat(0).take(len));
49+ ///
50+ /// let mut vec3 = Vec::new();
51+ /// vec3.resize(len, 0);
3552 /// ```
3653 ///
3754 /// Use instead:
3855 /// ```rust
3956 /// # let len = 4;
4057 /// let mut vec1 = vec![0; len];
4158 /// let mut vec2 = vec![0; len];
59+ /// let mut vec3 = vec![0; len];
4260 /// ```
4361 #[ clippy:: version = "1.32.0" ]
4462 pub SLOW_VECTOR_INITIALIZATION ,
0 commit comments