Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Describe efficiently creating a Vec of zeroes #48

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/standard-library-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -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; lenght]`(https://doc.rust-lang.org/stable/std/macro.vec.html).
Shnatsel marked this conversation as resolved.
Show resolved Hide resolved
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
Expand Down