File tree 1 file changed +15
-1
lines changed
1 file changed +15
-1
lines changed Original file line number Diff line number Diff line change 10
10
11
11
//! Shareable mutable containers.
12
12
//!
13
+ //! Rust memory safety is based on this rule: Given an object `T`, is only possible to
14
+ //! have one of the following:
15
+ //!
16
+ //! - Having several inmutable references (`&T`) to the object (also know as Aliasing).
17
+ //! - Having one mutable reference (`&mut T`) to the object (also know as Mutability).
18
+ //!
19
+ //! This is enforced by the Rust compiler. However, there are situations where this rule is not
20
+ //! flexible enough. Sometimes is required to have multiple references to an object and yet
21
+ //! mutate it.
22
+ //!
23
+ //! Shareable mutable containers exist to permit mutability in presence of aliasing in a
24
+ //! controlled manner. Both `Cell<T>` and `RefCell<T>` allows to do this in a single threaded
25
+ //! way. For multiple threads is possible to use `Mutex`, `RwLock` or `AtomicXXX`.
26
+ //!
13
27
//! Values of the `Cell<T>` and `RefCell<T>` types may be mutated through shared references (i.e.
14
28
//! the common `&T` type), whereas most Rust types can only be mutated through unique (`&mut T`)
15
29
//! references. We say that `Cell<T>` and `RefCell<T>` provide 'interior mutability', in contrast
16
- //! with typical Rust types that exhibit 'inherited mutability'.
30
+ //! with typical Rust types that exhibit 'inherited mutability'.
17
31
//!
18
32
//! Cell types come in two flavors: `Cell<T>` and `RefCell<T>`. `Cell<T>` implements interior
19
33
//! mutability by moving values in and out of the `Cell<T>`. To use references instead of values,
You can’t perform that action at this time.
0 commit comments