October 2018
Beginner
180 pages
4h 48m
English
Cell's semantics of moving the stored data value in and out of the cell are not always convenient to work with, and for large data values, moving them can be an expensive operation that we don't want to keep repeating over and over without need. RefCell to the rescue!
The RefCell type supports RefCell::new, replace, and into_inner, just as Cell does, but it also has functions that allow us to borrow the contained value, either mutably or immutably.
Let's give RefCell a whirl:
let refcell = RefCell::new("It's a string".to_string()); match refcell.try_borrow() { Ok(x) => { println!("Borrowed: {}", x); } Err(_) => { println!("Couldn't borrow first try"); } }; let borrowed_mutably = refcell.try_borrow_mut()?; match refcell.try_borrow() ...Read now
Unlock full access