我有一个hashmap:("a",1),("b",2),("c",3),现在我想使用iter_mut()方法加倍每个值。但是,该示例使用循环。像这样:for (_, val) in map.iter_mut() { *val *= 2; },如果没有for循环,我怎么能做同样的事情呢?
发布于 2022-09-23 16:55:06
use std::collections::HashMap;
fn main() {
let mut map = HashMap::from([("a", 1), ("b", 2), ("c", 3)]);
map.iter_mut().for_each(|(_, val)| {
*val *= 2;
});
println!("{:?}", map);
}{"c": 6, "a": 2, "b": 4}https://stackoverflow.com/questions/73830636
复制相似问题