Cell and RefCell

Rust's rule that only one block of code can have write access to a data value at any one time is a good one, but sometimes the restrictions that are needed to be sure when the compiler is running that this rule will always be followed are too tight. Sometimes, we need the extra freedom that comes from having the rule checked while the program is running, instead.

The compiler checks would ensure that the program can't break the rule, while the runtime checks ensure that the program doesn't break the rule, giving us more flexibility at the cost of some overhead.

To support this option, Rust provides us with the Cell and RefCell data types, which are smart pointers that allow us to change their contents, even if they are not stored in a mutable variable.

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

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