std-method

Vec::capacity

Report how many elements can be stored before further allocation is needed. A zero-sized element vector reports usize::MAX. Do not rely on an exact growth factor. Expected stdout (not executed): 2 true Common mistake: Confusing available slots with capacity: available slots are capacity minus length.

Formal Syntax & Structure

rust syntaxrust
pub fn capacity(&self) -> usize

Working Example

Example Snippetrust
fn main() {    let mut v = Vec::with_capacity(4);    v.extend([1, 2]);    println!("{} {}", v.len(), v.capacity() - v.len() >= 2);}
Rust Reference