Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::rc::Rc;
- fn main() {
- let mut li = List::new();
- li.prepend(9000);
- li.prepend(777);
- li.prepend(42);
- }
- struct List {
- head: Option<Rc<Node>>,
- last: Option<Rc<Node>>,
- }
- impl List {
- fn new() -> Self {
- Self {
- head: None,
- last: None,
- }
- }
- fn prepend(&mut self, value: i32) {
- self.head = Some(Rc::new(Node {
- value,
- next: self.head.take(),
- }));
- if self.last.is_none() {
- self.last = self.head.clone();
- }
- }
- }
- struct Node {
- value: i32,
- next: Option<Rc<Node>>,
- }
- impl Drop for Node {
- fn drop(&mut self) {
- println!("Droppe Node ({})", self.value);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement