Skip to content

Commit 20f6aa1

Browse files
authored
Rollup merge of rust-lang#111592 - Nemo157:fix-vec-capacity-examples, r=joshtriplett
Change Vec examples to not assert exact capacity except where it is guaranteed It was [brought up on discord](https://discord.com/channels/273534239310479360/818964227783262209/1107633959329878077) that the `Vec::into_boxed_slice` example contradicted the `Vec::with_capacity` docs in that the returned `Vec` might have _more_ capacity than requested. So, to reduce confusion change all the `assert_eq!(vec.capacity(), _)` to `assert!(vec.capacity() >= _)`, except in 4 examples that have guaranteed capacities: `Vec::from_raw_parts`, `Vec::from_raw_parts_in`, `Vec::<()>::with_capacity`,`Vec::<(), _>::with_capacity_in`.
2 parents 6dc3650 + a9cb482 commit 20f6aa1

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

library/alloc/src/vec/mod.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -646,14 +646,14 @@ impl<T, A: Allocator> Vec<T, A> {
646646
///
647647
/// // The vector contains no items, even though it has capacity for more
648648
/// assert_eq!(vec.len(), 0);
649-
/// assert_eq!(vec.capacity(), 10);
649+
/// assert!(vec.capacity() >= 10);
650650
///
651651
/// // These are all done without reallocating...
652652
/// for i in 0..10 {
653653
/// vec.push(i);
654654
/// }
655655
/// assert_eq!(vec.len(), 10);
656-
/// assert_eq!(vec.capacity(), 10);
656+
/// assert!(vec.capacity() >= 10);
657657
///
658658
/// // ...but this may make the vector reallocate
659659
/// vec.push(11);
@@ -877,7 +877,7 @@ impl<T, A: Allocator> Vec<T, A> {
877877
/// ```
878878
/// let mut vec: Vec<i32> = Vec::with_capacity(10);
879879
/// vec.push(42);
880-
/// assert_eq!(vec.capacity(), 10);
880+
/// assert!(vec.capacity() >= 10);
881881
/// ```
882882
#[inline]
883883
#[stable(feature = "rust1", since = "1.0.0")]
@@ -1028,7 +1028,7 @@ impl<T, A: Allocator> Vec<T, A> {
10281028
/// ```
10291029
/// let mut vec = Vec::with_capacity(10);
10301030
/// vec.extend([1, 2, 3]);
1031-
/// assert_eq!(vec.capacity(), 10);
1031+
/// assert!(vec.capacity() >= 10);
10321032
/// vec.shrink_to_fit();
10331033
/// assert!(vec.capacity() >= 3);
10341034
/// ```
@@ -1055,7 +1055,7 @@ impl<T, A: Allocator> Vec<T, A> {
10551055
/// ```
10561056
/// let mut vec = Vec::with_capacity(10);
10571057
/// vec.extend([1, 2, 3]);
1058-
/// assert_eq!(vec.capacity(), 10);
1058+
/// assert!(vec.capacity() >= 10);
10591059
/// vec.shrink_to(4);
10601060
/// assert!(vec.capacity() >= 4);
10611061
/// vec.shrink_to(0);
@@ -1090,7 +1090,7 @@ impl<T, A: Allocator> Vec<T, A> {
10901090
/// let mut vec = Vec::with_capacity(10);
10911091
/// vec.extend([1, 2, 3]);
10921092
///
1093-
/// assert_eq!(vec.capacity(), 10);
1093+
/// assert!(vec.capacity() >= 10);
10941094
/// let slice = vec.into_boxed_slice();
10951095
/// assert_eq!(slice.into_vec().capacity(), 3);
10961096
/// ```

0 commit comments

Comments
 (0)