I’m very new to rust, attempting to create some ADTs to learn the ropes. I’m kinda stuck on trying to return the popped node from a linked list. As I understand, I’m mutably borrowing current.next
in the loop so when I then attempt to take()
it, the borrow checker flips out. I tried gpt, copilot and other sources but can’t seem to figure this out. Any tips on how to fix this would be greatly appreciated. Is this even a good approach in rust?
fn pop(&mut self) -> Option<Box<Node>> {
if self.head.is_none() {
return None;
}
if self.head.as_mut().unwrap().next.is_none() {
return self.head.take();
}
let mut current = self.head.as_mut().unwrap();
while let Some(ref mut node) = current.next {
if node.next.is_none() {
return current.next.take();
}
current = node;
}
None
}