Skip to content

Hasher::finish() does not reset the hasher. #43763

Closed
@partim

Description

@partim

At least for HashMap’s default hasher, a call to Hasher::finish() will not reset the hasher’s internal state. Instead, additional write()s will modify the hasher’s existing state as if finish() had never been called:

use std::hash::{Hash, Hasher};
use std::collections::hash_map::DefaultHasher;

fn main() {
    let mut hasher = DefaultHasher::new();
    0u8.hash(&mut hasher);
    let h1 = hasher.finish();
    0u8.hash(&mut hasher);
    let h2 = hasher.finish();
    println!("h1: {}\nh2: {}", h1, h2);
}

will produce

h1: 7541581120933061747
h2: 6165216591721696495

Is this intended behavior? If so, this should probably mentioned in the documentation as it is somewhat surprising given the name of the method. I am happy to create a PR but wanted to check first.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-feature-requestCategory: A feature request, i.e: not implemented / a PR.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions