Skip to content

Clarify documentation on HashMap::capacity() #24591

@tomjakubowski

Description

@tomjakubowski

The documentation for HashMap::capacity() says:

Returns the number of elements the map can hold without reallocating.

However, a HashMap will sometimes store more elements than its capacity indicates. See this example (Playpen link):

use std::collections::HashMap;

fn main() {
    let mut map = HashMap::new();
    for i in 0..1000 {
        map.insert(i, i);
        let (cap, len) = (map.capacity(), map.len());
        assert!(cap >= len, "cap {} len {}", cap, len);
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions