Skip to content

Instantly share code, notes, and snippets.

@milibopp
Created March 5, 2014 14:14
Show Gist options
  • Select an option

  • Save milibopp/9367932 to your computer and use it in GitHub Desktop.

Select an option

Save milibopp/9367932 to your computer and use it in GitHub Desktop.
Mutable reference counted objects
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