Guest User

Untitled

a guest
May 26th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. use std::rc::Rc;
  2. use std::cell::RefCell;
  3.  
  4. #[derive(Debug)]
  5. struct Node {
  6. value: i32,
  7. next: Option<Rc<RefCell<Node>>>,
  8. prev: Option<Rc<RefCell<Node>>>,
  9. }
  10.  
  11. impl Node {
  12. pub fn new (value: i32, next: Option<Rc<RefCell<Node>>>, prev: Option<Rc<RefCell<Node>>>) -> Node {
  13. Node {
  14. value: value,
  15. next: next,
  16. prev: prev,
  17. }
  18. }
  19.  
  20. pub fn set_next(&mut self, next: Rc<RefCell<Node>>) {
  21. self.next = Some(next)
  22. }
  23.  
  24. pub fn set_prev(&mut self, prev: Rc<RefCell<Node>>) {
  25. self.prev = Some(prev)
  26. }
  27. }
  28.  
  29. fn main() {
  30. let one = Node::new(1, None, None);
  31. let two = Node::new(2, None, None);
  32.  
  33. let two_rc = Rc::new(RefCell::new(two));
  34. let one_rc = Rc::new(RefCell::new(one));
  35. let one_rc2 = Rc::clone(&one_rc);
  36. let two_rc2 = Rc::clone(&two_rc);
  37.  
  38.  
  39. println!("one_rc = {:?}", one_rc.borrow_mut());
  40. one_rc.borrow_mut().set_next(Rc::clone(&two_rc2));
  41. println!("one_rc = {:?}", one_rc.borrow_mut());
  42.  
  43. println!("two_rc = {:?}", two_rc.borrow_mut());
  44. two_rc.borrow_mut().set_prev(Rc::clone(&one_rc2));
  45. println!("two_rc = {:?}", two_rc.borrow_mut());
  46. }
Add Comment
Please, Sign In to add comment