Rust program to print the capacity of HashMap

Rust | HashMap Program: Write a program to print the capacity of HashMap.
Submitted by Nidhi, on October 17, 2021

Problem Solution:

In this program, we will create a HashMap and insert some items into it. Then we get the capacity of HashMap using the capacity() method. The capacity of a HashMap will be multiple of 7 by default.

Program/Source Code:

The source code to print the capacity of HashMap is given below. The given program is compiled and executed successfully.

// Rust program to print the 
// capacity of HashMap

use std::collections::HashMap;

fn main() 
{
    let mut map = HashMap::new();
    
    map.insert("Key1", 101);
    map.insert("Key2", 102);
    map.insert("Key3", 103);
    map.insert("Key4", 104);

    println!("Capacity of HashMap: {}",map.capacity());
}

Output:

Capacity of HashMap: 7

Explanation:

Here, we created a HashMap. Then we inserted some items into it. After that, we got the capacity of HashMap using the capacity() method and printed the result.

Rust HashMap Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.