Created
March 5, 2014 14:14
-
-
Save milibopp/9367932 to your computer and use it in GitHub Desktop.
Mutable reference counted objects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use std::rc::Rc; | |
| use std::cell::{RefMut, RefCell}; | |
| struct Stuff{ | |
| i: int, | |
| } | |
| fn main() { | |
| // Allocate ref-counted stuff | |
| let rcref: Rc<RefCell<Stuff>> = Rc::new(RefCell::new(Stuff{i: 1})); | |
| // Modify stuff | |
| let mut foo: RefMut<Stuff> = rcref.borrow().borrow_mut(); | |
| let bar: &mut Stuff = foo.get(); | |
| bar.i = 2; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment