I'm new to rust and try to understand &mut ref variables and mutability. I started creating a simple link list with pop_back function.
pub fn pop_back(&mut self) -> Option<T> {
let mut head = &mut self.head;
while let Some(v) = head {
if v.next.is_none() {
break;
}
head = &mut v.next;
}
head.take().map(|node| node.data)
}
but can't make it to work. error is cannot borrow *head as mutable more than once at a time
.
How can I tell rust that I want to only change the reference in my loop not the value?
I don't want to add another tail variable to my list so without changing structure how can I make this work?
this is the struct definition
pub struct Node<T> {
data: T,
next: Option<Box<Node<T>>>
}
pub struct SimpleLinkedList<T> {
head: Option<Box<Node<T>>>,
}