Description
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?