Skip to content

Vec::with_capacity does not work correctly for zero-sized types #43490

Closed
@mistodon

Description

@mistodon

Calling Vec::with_capacity(N) does not give you a vector with capacity N.

fn main()
{
    let v: Vec<()> = Vec::with_capacity(5);
    assert_eq!(v.capacity(), 5);
}

This fails with:

thread 'main' panicked at 'assertion failed: `(left == right)` (left: `18446744073709551615`, right: `5`)', src/main.rs:4

when running in the playground on stable.

I would expect the capacity to be 5. Logically, I realize the capacity for a zero-sized type is not actually limited, but the docs do say "vec![x; n], vec![a, b, c, d], and Vec::with_capacity(n), will all produce a Vec with exactly the requested capacity."

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsC-bugCategory: This is a bug.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions