std-method
Vec::reserve
Ensure capacity for at least len + additional elements, possibly allocating extra capacity. Existing elements and length are preserved. For recoverable allocation failure use try_reserve. Expected stdout (not executed): 2 true Common mistake: Passing a desired total capacity instead of the additional element count.
Formal Syntax & Structure
rust syntaxrust
pub fn reserve(&mut self, additional: usize)Working Example
Example Snippetrust
fn main() { let mut values = vec![1, 2]; values.reserve(3); println!("{} {}", values.len(), values.capacity() >= 5);}