Skip to content

Commit d41e599

Browse files
committed
docs: clarify String/Vec docs related to capacity
* adds a note to String::shrink_to_fit to match the same note on Vec regarding additional capacity * change asserts in docs to treat capacity as a lower bound
1 parent 2e48b59 commit d41e599

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

src/libcollections/string.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,11 @@ impl String {
763763
self.vec.reserve_exact(additional)
764764
}
765765

766-
/// Shrinks the capacity of this string buffer to match its length.
766+
/// Shrinks the capacity of this string buffer to match its length as much
767+
/// as possible.
768+
///
769+
/// It will drop down as close as possible to the length but the allocator
770+
/// may still inform the string that there is space for a few more bytes.
767771
///
768772
/// # Examples
769773
///
@@ -772,7 +776,7 @@ impl String {
772776
/// s.reserve(100);
773777
/// assert!(s.capacity() >= 100);
774778
/// s.shrink_to_fit();
775-
/// assert_eq!(s.capacity(), 3);
779+
/// assert!(s.capacity() >= 3);
776780
/// ```
777781
#[inline]
778782
#[stable(feature = "rust1", since = "1.0.0")]

src/libcollections/vec.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,10 +273,13 @@ impl<T> Vec<T> {
273273
/// assert_eq!(vec.len(), 0);
274274
///
275275
/// // These are all done without reallocating...
276+
/// let cap = vec.capacity();
276277
/// for i in 0..10 {
277278
/// vec.push(i);
278279
/// }
279280
///
281+
/// assert_eq!(vec.capacity(), cap);
282+
///
280283
/// // ...but this may make the vector reallocate
281284
/// vec.push(11);
282285
/// ```
@@ -349,7 +352,7 @@ impl<T> Vec<T> {
349352
///
350353
/// ```
351354
/// let vec: Vec<i32> = Vec::with_capacity(10);
352-
/// assert_eq!(vec.capacity(), 10);
355+
/// assert!(vec.capacity() >= 10);
353356
/// ```
354357
#[inline]
355358
#[stable(feature = "rust1", since = "1.0.0")]
@@ -411,7 +414,7 @@ impl<T> Vec<T> {
411414
/// ```
412415
/// let mut vec = Vec::with_capacity(10);
413416
/// vec.extend([1, 2, 3].iter().cloned());
414-
/// assert_eq!(vec.capacity(), 10);
417+
/// assert!(vec.capacity() >= 10);
415418
/// vec.shrink_to_fit();
416419
/// assert!(vec.capacity() >= 3);
417420
/// ```

0 commit comments

Comments
 (0)