Guest User

Untitled

a guest
Jun 22nd, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. use std::mem;
  2.  
  3.  
  4. fn main() {
  5. #[derive(Debug)]
  6. struct Node {
  7. elem: i32,
  8. next: Link,
  9. }
  10.  
  11. #[derive(Debug)]
  12. enum Link {
  13. Empty,
  14. More(Box<Node>),
  15. }
  16.  
  17. #[derive(Debug)]
  18. pub struct List {
  19. head: Link,
  20. }
  21.  
  22.  
  23. impl List {
  24. pub fn new() -> Self {
  25. List { head: Link::Empty }
  26. }
  27.  
  28. pub fn push(&mut self, elem: i32) {
  29. let new_node = Box::new(Node {
  30. elem: elem,
  31. next: mem::replace(&mut self.head, Link::Empty),
  32. });
  33.  
  34. self.head = Link::More(new_node);
  35. }
  36.  
  37. pub fn pop(&mut self) -> Option<i32> {
  38. match mem::replace(&mut self.head, Link::Empty) {
  39. Link::Empty => None,
  40. Link::More(node) => {
  41. self.head = node.next;
  42. Some(node.elem)
  43. }
  44. }
  45. }
  46.  
  47.  
  48. }
  49. }
Add Comment
Please, Sign In to add comment