Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. use std::rc::Rc;
  2.  
  3. fn main() {
  4. let mut li = List::new();
  5. li.prepend(9000);
  6. li.prepend(777);
  7. li.prepend(42);
  8. }
  9.  
  10. struct List {
  11. head: Option<Rc<Node>>,
  12. last: Option<Rc<Node>>,
  13. }
  14.  
  15. impl List {
  16. fn new() -> Self {
  17. Self {
  18. head: None,
  19. last: None,
  20. }
  21. }
  22.  
  23. fn prepend(&mut self, value: i32) {
  24. self.head = Some(Rc::new(Node {
  25. value,
  26. next: self.head.take(),
  27. }));
  28.  
  29. if self.last.is_none() {
  30. self.last = self.head.clone();
  31. }
  32. }
  33. }
  34.  
  35. struct Node {
  36. value: i32,
  37. next: Option<Rc<Node>>,
  38. }
  39.  
  40. impl Drop for Node {
  41. fn drop(&mut self) {
  42. println!("Droppe Node ({})", self.value);
  43. }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement