Rollup merge of #72986 - pickfire:vec-assert, r=Mark-Simulacrum

Add more assert to Vec with_capacity docs

Show assertion on len too to show them how adding new items will affect both the
length and capacity, before and after.
This commit is contained in:
Dylan DPC 2020-06-04 21:31:19 +02:00 committed by GitHub
commit e324c25fff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -343,15 +343,18 @@ impl<T> Vec<T> {
/// ///
/// // The vector contains no items, even though it has capacity for more /// // The vector contains no items, even though it has capacity for more
/// assert_eq!(vec.len(), 0); /// assert_eq!(vec.len(), 0);
/// assert_eq!(vec.capacity(), 10);
/// ///
/// // These are all done without reallocating... /// // These are all done without reallocating...
/// for i in 0..10 { /// for i in 0..10 {
/// vec.push(i); /// vec.push(i);
/// } /// }
/// assert_eq!(vec.len(), 10);
/// assert_eq!(vec.capacity(), 10); /// assert_eq!(vec.capacity(), 10);
/// ///
/// // ...but this may make the vector reallocate /// // ...but this may make the vector reallocate
/// vec.push(11); /// vec.push(11);
/// assert_eq!(vec.len(), 11);
/// assert!(vec.capacity() >= 11); /// assert!(vec.capacity() >= 11);
/// ``` /// ```
#[inline] #[inline]