Skip to content

Instantly share code, notes, and snippets.

@HomoEfficio
Last active July 16, 2019 14:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HomoEfficio/db54e5781235e32b1d64ef83b190f8b5 to your computer and use it in GitHub Desktop.
Save HomoEfficio/db54e5781235e32b1d64ef83b190f8b5 to your computer and use it in GitHub Desktop.
use std::rc::Rc;
use std::cell::RefCell;
#[derive(Debug)]
struct MyType {
num: i32
}
impl MyType {
pub fn add(&mut self, amount: i32) {
self.num += amount;
}
}
fn main() {
let my_type = MyType { num: 10 };
println!("{:?}", my_type);
let rfc_my_type: RefCell<MyType> = RefCell::new(my_type);
rfc_my_type.borrow_mut().add(1);
println!("{:?}", rfc_my_type.borrow());
let rc1_my_type: Rc<&RefCell<MyType>> = Rc::new(&rfc_my_type);
rc1_my_type.borrow_mut().add(1);
println!("{:?}", rc1_my_type.borrow());
let rc2_my_type: Rc<&RefCell<MyType>> = Rc::new(&rfc_my_type);
rc2_my_type.borrow_mut().add(1);
println!("{:?}", rc2_my_type.borrow());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment