struct Mylist { v: i32, next: Option> } impl Mylist { fn new(val: i32) -> std::rc::Rc { std::rc::Rc::new(Mylist{v: val, next: None}) } fn append(val: i32, l: std::rc::Rc) -> std::rc::Rc { std::rc::Rc::new(Mylist{v: val, next: Some(l)}) } } 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.clone()); { let l3 = Mylist::append(7, l2.clone()); println!("l3: {}", l3.v); } println!("l2: {}", l2.v); if let Some(next) = l2.next.clone() { println!("l2->next: {}", next.v); } else { println!("l2->next is empty!"); } }