std-method

HashMap::new

Start with an empty map using the default hasher. Storage is allocated as needed when entries are added. Use a type annotation if inference has no key or value information. Expected stdout (not executed): true 1 Common mistake: Treating a missing key as an implicit default value.

Formal Syntax & Structure

rust syntaxrust
pub fn new() -> HashMap<K, V>

Working Example

Example Snippetrust
fn main() {    use std::collections::HashMap;    let mut totals: HashMap<&str, u32> = HashMap::new();    println!("{}", totals.is_empty());    totals.insert("tea", 2);    println!("{}", totals.len());}
Rust Reference