Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::rc::Rc;
- use std::cell::RefCell;
- #[derive(Debug)]
- struct Node {
- value: i32,
- next: Option<Rc<RefCell<Node>>>,
- prev: Option<Rc<RefCell<Node>>>,
- }
- impl Node {
- pub fn new (value: i32, next: Option<Rc<RefCell<Node>>>, prev: Option<Rc<RefCell<Node>>>) -> Node {
- Node {
- value: value,
- next: next,
- prev: prev,
- }
- }
- pub fn set_next(&mut self, next: Rc<RefCell<Node>>) {
- self.next = Some(next)
- }
- pub fn set_prev(&mut self, prev: Rc<RefCell<Node>>) {
- self.prev = Some(prev)
- }
- }
- fn main() {
- let one = Node::new(1, None, None);
- let two = Node::new(2, None, None);
- let two_rc = Rc::new(RefCell::new(two));
- let one_rc = Rc::new(RefCell::new(one));
- let one_rc2 = Rc::clone(&one_rc);
- let two_rc2 = Rc::clone(&two_rc);
- println!("one_rc = {:?}", one_rc.borrow_mut());
- one_rc.borrow_mut().set_next(Rc::clone(&two_rc2));
- println!("one_rc = {:?}", one_rc.borrow_mut());
- println!("two_rc = {:?}", two_rc.borrow_mut());
- two_rc.borrow_mut().set_prev(Rc::clone(&one_rc2));
- println!("two_rc = {:?}", two_rc.borrow_mut());
- }
Add Comment
Please, Sign In to add comment