I get a Issue about borrowed check, as follows code
fn remove_node(&mut self, val: i32) {
let mut current_node = &mut self.head;
while let Some(node) = current_node {
if node.val == val {
*current_node = node.next.take();
break;
} else {
current_node = &mut node.next;
}
}
}
I get a error message(" `*current_node` is assigned to here but it was already borrowed, borrow later used here"). if remove else block code or if block code, i will don't get error. I don't know why.
You can read more about it in this crate. Sometimes it can be worked around (expand the Non-unsafe section), though I don't have time just now to try to apply it to your case myself. Or the crate itself may work for you.
Thanks your answer. I now know the cause of the problem. use the borrowed value from current_node later, so don't allow to update it. I now think how to resolve problem with a better plan. but no result yet.
If this is just for learning Rust, of course use one of the proposed solutions. But if you really need linked lists: you do know that the stdlib has a LinkedList type, right? See LinkedList in std::collections - Rust.