Skip to content

The documentation of Vec contradicts itself regarding with_capacity #101316

Closed

Description

@Kixunil

Location

https://doc.rust-lang.org/std/vec/struct.Vec.html

Summary

In section Guarantees the documentation says:

vec![x; n], vec![a, b, c, d], and Vec::with_capacity(n), will all produce a Vec with exactly the requested capacity.

However in Vec::with_capacity(n) it says:

This method is allowed to allocate for more elements than capacity.

And

If it is imporant to know the exact allocated capacity of a Vec, always use the capacity method after construction.

These clearly contradict each-other. I guess it's better for performance to not guarantee capacity but maybe it'd break someones code to change it since it was already "documented". :(

Activity

  1. added
    A-docsArea: Documentation for any part of the project, including the compiler, standard library, and tools
    on Sep 2, 2022
  2. ehuss commented on Sep 2, 2022

    @ehuss
    Contributor

    I believe this is being worked on in #99790. Can you take a look at that PR and comment over there if it still seems unclear?

  3. Kixunil commented on Sep 2, 2022

    @Kixunil
    ContributorAuthor

    Heh, was trying to find an issue but didn't think of looking at PRs. Will keep open until that merges.

  4. added
    T-libs-api[DEPRECATED; DO NOT USE]
    E-help-wantedCall for participation: Help is requested to fix this issue.
    on Mar 27, 2025
  5. jogru0 commented on Apr 19, 2025

    @jogru0
    Contributor

    The documentation now says:

    vec![x; n], vec![a, b, c, d], and Vec::with_capacity(n), will all produce a Vec with at least the requested capacity.

    So I think the contradiction in the documentation is resolved and this issue can be closed, no?

  6. Kixunil commented on Apr 19, 2025

    @Kixunil
    ContributorAuthor

    Oh, yes, I've seen that PR merged but forgot to close this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsE-help-wantedCall for participation: Help is requested to fix this issue.T-libs-api[DEPRECATED; DO NOT USE]

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions