【发布时间】:2017-03-05 16:58:43
【问题描述】:
这个sn-p
use std::collections::HashMap;
struct Foo {
local_ids: HashMap<i32, i32>,
last_id: i32,
}
impl Foo {
fn foo(&mut self, external_id: i32) {
let id = self.local_ids
.entry(external_id)
.or_insert_with(||{self.last_id += 1; self.last_id});
}
}
不起作用,因为我们不能借用 self 两次
error: closure requires unique access to `self` but `self.local_ids` is already borrowed [E0500]
是否可以在不进行第二次密钥查找的情况下解决此问题?
这与Rust: HashMap borrow issue when trying to implement find or insert 非常相似,但API 发生了很大变化。
上面的 find_with_or_insert_with 答案似乎没有映射到当前的 api。
【问题讨论】:
标签: dictionary rust