I am using a HashMap
to store an enum. I'd like to get a value from the HashMap
and if the value is a specific enum variant, I'd like to insert a modified copy of the value back in the HashMap
.
The code I came up with looks like this:
if let Node::LeafNode(mut leaf_node) = *(self.pages.get(&page).unwrap()) {
let mut leaf_node = leaf_node.clone();
// ...
self.pages.insert(leaf_page,Node::LeafNode(leaf_node));
}
This does not compile because the borrow of self.pages
lasts until the end of the if let
-block and self.pages.insert
is a mutable borrow.
I have tried to shadow the value of the HashMap
with a copy of the value, but this does not end the borrow. Usually I would use a {}
block to limit the borrow, but this seems to be not possible in match
or if let
.
What is the idiomatic way to end a borrow so that I can get a new mutable borrow?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…