Created
October 18, 2022 10:44
-
-
Save baakeydow/ec2c0f97b1270c1cbb1c1433e052f1bc to your computer and use it in GitHub Desktop.
#rust multiple owners with Rc + RefCell
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
fn multiple_owners() { | |
use std::rc::Rc; | |
use std::cell::RefCell; | |
let value = Rc::new(RefCell::new(5)); | |
let a = Rc::clone(&value); | |
let b = Rc::clone(&value); | |
*a.borrow_mut() += 1; | |
*b.borrow_mut() += 1; | |
assert_eq!(*value.borrow(), 7); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment