Skip to content

Commit 15bec3d

Browse files
authored
Rollup merge of #39701 - sgrif:sg-vec-reserve-docs, r=alexcrichton
Explicitly mention that `Vec::reserve` is based on len not capacity I spent a good chunk of time tracking down a buffer overrun bug that resulted from me mistakenly thinking that `reserve` was based on the current capacity not the current length. It would be helpful if this were called out explicitly in the docs.
2 parents 92cf7ae + b3937ea commit 15bec3d

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

src/libcollections/vec.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,9 @@ impl<T> Vec<T> {
437437

438438
/// Reserves capacity for at least `additional` more elements to be inserted
439439
/// in the given `Vec<T>`. The collection may reserve more space to avoid
440-
/// frequent reallocations.
440+
/// frequent reallocations. After calling `reserve`, capacity will be
441+
/// greater than or equal to `self.len() + additional`. Does nothing if
442+
/// capacity is already sufficient.
441443
///
442444
/// # Panics
443445
///
@@ -456,8 +458,9 @@ impl<T> Vec<T> {
456458
}
457459

458460
/// Reserves the minimum capacity for exactly `additional` more elements to
459-
/// be inserted in the given `Vec<T>`. Does nothing if the capacity is already
460-
/// sufficient.
461+
/// be inserted in the given `Vec<T>`. After calling `reserve_exact`,
462+
/// capacity will be greater than or equal to `self.len() + additional`.
463+
/// Does nothing if the capacity is already sufficient.
461464
///
462465
/// Note that the allocator may give the collection more space than it
463466
/// requests. Therefore capacity can not be relied upon to be precisely

0 commit comments

Comments
 (0)