问题描述
我有一个HashMap
,想更新一个值(如果存在),否则添加一个默认值。通常我会这样:
some_map.entry(some_key)
.and_modify(|e| modify(e))
.or_insert(default)
但是现在我的modify
的类型为fn(T)->T
,但是借位检查器显然不允许我写:
some_map.entry(some_key)
.and_modify(|e| *e = modify(*e))
.or_insert(default)
在Rust中执行此操作的首选方式是什么?我应该只使用remove
和insert
吗?
解决方法
假设您可以便宜地创建T
的空版本,则可以使用mem::replace
:
some_map.entry(some_key)
.and_modify(|e| {
// swaps the second parameter in and returns the value which was there
let mut v = mem::replace(e,T::empty());
v = modify(v);
// puts the value back in and discards the empty one
mem::replace(e,v);
})
.or_insert(default)
这假设modify
不会出现恐慌,否则您将发现自己的“空”值保留在地图中。但是remove
/ insert
也会遇到类似的问题。