以下代码可用,但是由于
is_empty
的定义太远,使用起来不够美观。fn remove(&mut self, index: I, primary_key: &Rc<K>) {
let is_empty;
{
let ks = self.data.get_mut(&index).unwrap();
ks.remove(primary_key);
is_empty = ks.is_empty();
}
// I have to wrap `ks` in an inner scope so that we can borrow `data` mutably.
if is_empty {
self.data.remove(&index);
}
}
我们有没有一些方法在进入if分支之前删除条件中的变量,例如:
if {ks.is_empty()} {
self.data.remove(&index);
}
{ let key = occupied.get_mut();/*更改*/ }
中,然后进行检查/删除。 - Matthieu M.