Last active
July 16, 2019 14:33
-
-
Save HomoEfficio/db54e5781235e32b1d64ef83b190f8b5 to your computer and use it in GitHub Desktop.
This file contains 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::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