struct Mylist { v: i32, next: Option>>, prev: Option>> } impl Mylist { fn new(val: i32) -> std::rc::Rc> { std::rc::Rc::new(std::cell::RefCell::new(Mylist{v: val, next: None, prev: None})) } fn append(val: i32, l: &std::rc::Rc>) -> std::rc::Rc> { let res = std::rc::Rc::new(std::cell::RefCell::new(Mylist{v: val, next: Some(l.clone()), prev: None})); l.borrow_mut().prev = Some(res.clone()); res } } impl Drop for Mylist { fn drop(&mut self) { println!("Node with value {} is being destroyed!", self.v) } } fn main() { let l1 = Mylist::new(5); let l2 = Mylist::append(6, &l1); { let l3 = Mylist::append(7, &l2); println!("l3: {}", l3.borrow().v); } println!("l2: {}", l2.borrow().v); if let Some(next) = l2.borrow().next.clone() { println!("l2->next: {}", next.borrow().v); } else { println!("l2->next is empty!"); }; }