Skip to content

The documentation of Vec contradicts itself regarding with_capacity #101316

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

Closed
Kixunil opened this issue Sep 2, 2022 · 4 comments
Closed

The documentation of Vec contradicts itself regarding with_capacity #101316

Kixunil opened this issue Sep 2, 2022 · 4 comments
Labels
A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools E-help-wanted Call for participation: Help is requested to fix this issue. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@Kixunil
Copy link
Contributor

Kixunil commented Sep 2, 2022

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". :(

@Kixunil Kixunil added the A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools label Sep 2, 2022
@ehuss
Copy link
Contributor

ehuss commented Sep 2, 2022

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?

@Kixunil
Copy link
Contributor Author

Kixunil commented Sep 2, 2022

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

@lolbinarycat lolbinarycat added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. E-help-wanted Call for participation: Help is requested to fix this issue. labels Mar 27, 2025
@jogru0
Copy link
Contributor

jogru0 commented Apr 19, 2025

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?

@Kixunil
Copy link
Contributor Author

Kixunil commented Apr 19, 2025

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

@Kixunil Kixunil closed this as completed Apr 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools E-help-wanted Call for participation: Help is requested to fix this issue. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants