diff --git a/src/standard-library-types.md b/src/standard-library-types.md index 8be6d96..8a40ca2 100644 --- a/src/standard-library-types.md +++ b/src/standard-library-types.md @@ -41,6 +41,13 @@ not preserve ordering, but is O(1). equivalent method for other collection types such as `String`, `HashSet`, and `HashMap`. +If you need to create a `Vec` filled with zeroes, the fastest way to do so +is `vec![0; length]`(https://doc.rust-lang.org/stable/std/macro.vec.html). +Unlike creating a `Vec` and then filling it with zeroes, this requests +pre-zeroed memory from the OS, which usually has some on hand, +so you don't have to spend time zeroing it yourself. +This is the fastest way to obtain a zero-initialized `Vec` or slice. + [`Vec::remove`]: https://doc.rust-lang.org/std/vec/struct.Vec.html#method.remove [`Vec::swap_remove`]: https://doc.rust-lang.org/std/vec/struct.Vec.html#method.swap_remove [`Vec::retain`]: https://doc.rust-lang.org/std/vec/struct.Vec.html#method.retain