-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Clarify the std::vec::Vec::with_capacity docs #15852
Conversation
The referenced issue does not appear to be conflating length and capacity. If it were, it wouldn't use the In other words, I think this is a unnecessary attempt at clarifying the docs. It doesn't actually address the real confusion. Also, a single instance of confusion does not necessarily mean docs need changing anyway; even the clearest of documentation can still result in the occasional confused reader. |
@kballard I was under the impression that with_capacity indeed returns me a a vector of the given size. I think clarifying capacity and size at a place like this makes sense and helps people that are new to the language. |
@kballard In a related discussion on IRC just after the referenced issue was filed, another person agreed that with_capacity is confusing, and that a reasonable solution would be to update the docs. This is what prompted me to make this PR. |
The name is self-explanatory. I think this could be clarified in a tutorial. You could mention that |
@pczarn The name is self-explanatory (in a sense), but that’s part of the problem—it might explain itself incorrectly. Anyone who doesn’t yet know of the difference between capacity and length could easily presume that The sort of scenario I’m trying to remedy is when somebody is looking for a way to create a vector with a given length, and looks for a name along the lines of I’m not trying to say that the above situation happens for every person looking for a function. If someone was looking for a function to create a vector from a given element, for example, they’d be quite pleased to find the right function for the job, |
The notion of a "capacity" is referenced not just from the While it may be a good idea to include a reminder to the reader of the doc for |
@pnkfelix Good point. I’ve moved the capacity definition to the |
There is some confusion (cc #15851) about what
Vec::with_capacity
actually does—someone who doesn’t understand the difference between capacity and length (an understandable conflation) could easily presume that it creates a new vector with the specified length (filled up with some kind of default or undefined value).