Interior mutability

The opposite (interior mutability) can be found in this example:

use std::cell::RefCell; 
fn main() 
{ 
    let x = RefCell::new(42); 
    let y = x.borrow_mut(); 
} 

Here, RefCell gives the &mut when borrow_mut() is called. It works well, but will cause a panic if a second borrow_mut() is called on x; you are only allowed a single reference to a mutable.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset