Skip to content

The insert_or_update_with method on hashmaps has a superfluous parameter #9995

Closed
@bstrie

Description

@bstrie

insert_or_update_with attempts to insert a key/value pair into a hashmap, but if the key already exists, it allows you to update the value according to a closure (this is almost universally used to increment a counter... perhaps we need a counter type in libextra). This closure has two parameters, a key and a value. However, the key parameter to the closure is superfluous, as you've already provided the key in question to the method and can simply close over it if you want to reference it. Here's a demonstration:

use std::hashmap::HashMap;
fn main() {
    let mut map = HashMap::<uint,uint>::new();
    let k = 1;
    map.insert_or_update_with(k, k, |_, v| *v+=k);
    map.insert_or_update_with(k, k, |_, v| *v+=k);
}

We ignore the key parameter to the closure with _, and yet have no trouble referring to it via the closed-over value.

Am I wrong in believing that this is superfluous? Is there perhaps some borrowing-related complication that would warrant leaving this parameter in place?

Metadata

Metadata

Assignees

No one assigned

    Labels

    E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions