Skip to content

Instantly share code, notes, and snippets.

@bspeice
Created January 16, 2019 02:59
Show Gist options
  • Save bspeice/0391488c8ae06f4fbf34b7fb8a408c5d to your computer and use it in GitHub Desktop.
Save bspeice/0391488c8ae06f4fbf34b7fb8a408c5d to your computer and use it in GitHub Desktop.
RefCell replacement as normal
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