Skip to content

Commit

Permalink
Rollup merge of #100872 - JanBeh:PR_vec_default_alloc_doc, r=fee1-dead
Browse files Browse the repository at this point in the history
Add guarantee that Vec::default() does not alloc

Currently `Vec::new()` is guaranteed to not allocate until elements are pushed onto the `Vec`, but such a guarantee is missing for `Vec`'s implementation of `Default::default`.

This adds such a guarantee for `Vec::default()` to the API reference.

See also [this discussion on URLO](https://users.rust-lang.org/t/guarantee-that-vec-default-does-not-allocate/79903).
  • Loading branch information
Dylan-DPC committed Aug 22, 2022
2 parents 58d2373 + 0227b71 commit 4ed8fa4
Showing 1 changed file with 2 additions and 0 deletions.
2 changes: 2 additions & 0 deletions library/alloc/src/vec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2928,6 +2928,8 @@ unsafe impl<#[may_dangle] T, A: Allocator> Drop for Vec<T, A> {
#[rustc_const_unstable(feature = "const_default_impls", issue = "87864")]
impl<T> const Default for Vec<T> {
/// Creates an empty `Vec<T>`.
///
/// The vector will not allocate until elements are pushed onto it.
fn default() -> Vec<T> {
Vec::new()
}
Expand Down

0 comments on commit 4ed8fa4

Please sign in to comment.