Skip to content

Commit

Permalink
Add a paragraph about creating zero-filled vectors.
Browse files Browse the repository at this point in the history
Thanks to @Shnatsel for the suggestion in #48.
  • Loading branch information
nnethercote committed Dec 16, 2022
1 parent d53e4db commit 3e165fb
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/standard-library-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ copying it over.

## `Vec`

The best way to create a zero-filled `Vec` of length `n` is with `vec![0; n]`.
This is simple and probably [as fast or faster] than alternatives, such as
using `resize`, `extend`, or anything involving `unsafe`, because it can use OS
assistance.

[as fast or faster]: https://github.com/rust-lang/rust/issues/54628

[`Vec::remove`] removes an element at a particular index and shifts all
subsequent elements one to the left, which makes it O(n). [`Vec::swap_remove`]
replaces an element at a particular index with the final element, which does
Expand Down

0 comments on commit 3e165fb

Please sign in to comment.