Created
January 16, 2019 02:59
-
-
Save bspeice/0391488c8ae06f4fbf34b7fb8a408c5d to your computer and use it in GitHub Desktop.
RefCell replacement as normal
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::cell::RefCell; | |
fn my_mutator(cell: &RefCell<u8>) { | |
// Even though we're given an immutable reference, | |
// the `replace` method allows us to modify the inner value. | |
cell.replace(14); | |
} | |
fn main() { | |
let cell = RefCell::new(25); | |
// Prints out 25 | |
println!("Cell: {:?}", cell); | |
my_mutator(&cell); | |
// Prints out 14 | |
println!("Cell: {:?}", cell); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment