I have try to code a BST from scratch but there is some problem I found in my code that I don't understand, namely:
why this section resulted in error
fn _check_node(&mut self,checkval:i32)-> &mut Self{
if let Node::Cons(
ref curval,
ref mut high,
ref mut lows,
) = self{
if *curval > checkval{
return lows._check_node(checkval);
}else if *curval < checkval{
return high._check_node(checkval);
}
}
//high and lows should be out of scope here thus the previous borrow should no longer exist
//There is still comply error for some reason
//cannot borrow `*self` as mutable more than once at a time
return self;
}
You are right, this should be fine. This is a limitation of the current borrow checker. However, it will be made smarter in the future so that code like this can compile. (Search for "polonius" here.)
It's always a bit of a struggle to escape these issues but this seems to work. (I don't know exactly what your Node definition is, so I guessed). The first thing that is usually needed is to avoid the destructure match because that reborrows self, which is what causes the issues for the current version of the compiler. So changing that first conditional to return a bool avoids that. Similarly, the helper methods I added make it possible to get the inner data without using a destructure match.