-
Book Overview & Buying
-
Table Of Contents
The Rust Programming Handbook
By :
So far, we’ve been following Rust’s fundamental borrowing rule: at compile time, you can either have several immutable references (&T) to some data, or just one mutable reference (&mut T), but never both at the same time.
However, in some cases, compile-time enforcement may seem too restrictive. What if you want to change data accessed through an immutable reference or modify a value that has been immutably borrowed? This is when the interior mutability pattern becomes useful.
This principle is validated during compile time and is essential for Rust’s memory safety.
Nonetheless, there are scenarios in which this compile-time enforcement can seem overly constraining. What if you have a seemingly immutable value externally (you only have an &T), but you need to alter some internal component of it?
This is where the interior mutability pattern comes into play.