This is my singly linked list code have to implement pop from end and pop from front functions still, but I'm having difficulty in writing pop from end function
#![allow(warnings)] #[derive(Clone)] struct List { el: i32, next: Box<Option<List>> } impl List { fn new(val: i32) -> List { return List { el: val, next: Box::new(None) } } fn from(arr: &[i32]) -> List { let mut head = List::new(arr[0]); let mut current = &mut head; for &val in &arr[1..] { /* current.next = Box::new(Some(List::new(val))); current = (*current.next).as_mut().unwrap(); */ current.append(val); } return head } fn append(&mut self, val: i32) { let mut current = self; while let Some(ref mut next_node) = *current.next { current = next_node; } current.next = Box::new(Some(List::new(val))); } fn prepend(&mut self, val:i32) { // std::mem::replace(dest, src): // Moves src into the referenced dest, returning the previous dest value let old_self = std::mem::replace(self, List::new(val)); self.next = Box::new(Some(old_self)); } } fn main() { let mut list = List::new(42); list.append(32); list.append(2321); list.append(2839); list.prepend(69); list.pop(); // 2839 // difficulty in implementing this println!("{}", list); let mut list = List::from(&[1, 2, 3]); list.prepend(0); println!("{}", list); } Should I use Rc and RefCell to implement a singly linked list? I'm a beginner, so please help. Thanks!
popand the actual error message.