Rollup merge of #136293 - hkBst:patch-32, r=Amanieu

document capacity for ZST as example

The main text already covers this, although it provides weaker guarantees, but I think an example in the right spot does not hurt. Fixes #80747
This commit is contained in:
Jacob Pratt 2025-03-16 21:47:42 -04:00 committed by GitHub
commit c7700406f4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1252,6 +1252,19 @@ impl<T, A: Allocator> Vec<T, A> {
/// vec.push(42);
/// assert!(vec.capacity() >= 10);
/// ```
///
/// A vector with zero-sized elements will always have a capacity of usize::MAX:
///
/// ```
/// #[derive(Clone)]
/// struct ZeroSized;
///
/// fn main() {
/// assert_eq!(std::mem::size_of::<ZeroSized>(), 0);
/// let v = vec![ZeroSized; 0];
/// assert_eq!(v.capacity(), usize::MAX);
/// }
/// ```
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_stable(feature = "const_vec_string_slice", since = "CURRENT_RUSTC_VERSION")]