std-method

HashMap::reserve

Reserve enough capacity for at least len + additional entries; actual capacity can be greater. The requested count is extra entries, not bytes or total capacity. Capacity overflow can panic. Expected stdout (not executed): 2 true Common mistake: Passing the desired total length as though it were the additional count.

Formal Syntax & Structure

rust syntaxrust
pub fn reserve(&mut self, additional: usize)

Working Example

Example Snippetrust
fn main() {    use std::collections::HashMap;    let mut m = HashMap::from([("a", 1), ("b", 2)]);    m.reserve(5);    println!("{} {}", m.len(), m.capacity() >= 7);}
Rust Reference