Hi folks, fairly new to rust and still learning. I checked through the forum and std docs, but didn't see an example of any uses of and_modify on Entry for a multi-value variable. I specifically was working with HashMap's and really enjoyed the simplicity of the syntax:
my_map.entry(key_ref).and_modify(|my_val| val += 1).or_insert(1);
But when starting to create a hash map with tuple values I couldn't figure out how to modify two values in one line i.e.: :
use std::collections::HashMap;
let my_map: HashMap<char, (i32, i32)> = HashMap::new();
my_map.insert('c', (1, 1));
let my_key: char = 'c';
my_map.entry(my_key).and_modify(|my_val| my_val.0 += 1, my_val.1 *= 2).or_insert((1, 1));
or:
my_map.entry(my_key).and_modify(|(my_val1, my_val2)| *my_val1 += 1, *my_val2 *= 2).or_insert((1, 1));
both result in
error[E0061]: this method takes 1 argument but 2 arguments were supplied
while:
my_map.entry(my_key).and_modify(|my_val| (my_val.0 + 1, my_val.1 * 2)).or_insert((1, 1));
gives:
= note: expected unit type `()`
found tuple `(i32, i32)`
Since I assume its looking for an in-place operation with null tuple returned.
If the one liner isn't idiomatic what's the rustacean's opinionated approach here? get/entry to a local variable, modify, then insert?