Hey this is my code; PLAYGROUND
I am new to Rust. I don't understand borrows well as you can see.
Is it my lack of understanding?
In this case it's a shortcoming of the current borrow checker -- conditional return of a borrow. It thinks that this borrow:
match current_node.children.get_mut(key) {
Must always last for longer than the method body (for 'a
) because you sometimes return it. The current borrrow checker isn't sophisticated enough to decide something like "it borrows for 'a
if we return but perhaps less if we don't", or the like.
You can work around it in this case like so:
match current_node.children.get_mut(key) {
- Some(child) => {
+ Some(_) => {
// If child node found, update current_node and index]
- current_node = child;
+ current_node = current_node.children.get_mut(key).unwrap();
1 Like
Thank you
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.