I'm currently working on a linked list exercise. While implementing the push_tail
method, I wrote the following code:
pub struct LinkedList<T> {
head: Link<T>,
len: usize,
}
type Link<T> = Option<Box<Node<T>>>;
struct Node<T> {
data: T,
next: Link<T>,
}
// ...
impl<T> LinkedList<T> {
// ...
fn pop_tail(&mut self) -> Option<T> {
let mut current = &mut self.head;
while let Some(node) = current {
if node.next.is_none() {
break;
}
current = &mut node.next;
}
current.take().map(|node| {
self.len -= 1;
node.data
})
}
}
And the compiler reported the following error:
error[E0499]: cannot borrow `*current` as mutable more than once at a time
...
|
72 | while let Some(node) = current {
| ---- first mutable borrow occurs here
...
80 | current.take().map(|node| {
| ^^^^^^^
| |
| second mutable borrow occurs here
| first borrow later used here
I initially believed that the lifetime of the variable node
would end after the loop finishes, but it appears to be affecting the subsequent code. I even consulted an AI for help, but the explanation I received didn't make sense to me.
Could someone please help me understand what's going wrong?