Skip to content

Commit ee94252

Browse files
committed
Rollup merge of #48474 - christianpoveda:new_cell_docs, r=steveklabnik
New Cell docs This fixes #44061
2 parents fa374f9 + 9091584 commit ee94252

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/libcore/cell.rs

+18
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,24 @@
1010

1111
//! Shareable mutable containers.
1212
//!
13+
//! Rust memory safety is based on this rule: Given an object `T`, it is only possible to
14+
//! have one of the following:
15+
//!
16+
//! - Having several immutable references (`&T`) to the object (also known as **aliasing**).
17+
//! - Having one mutable reference (`&mut T`) to the object (also known as **mutability**).
18+
//!
19+
//! This is enforced by the Rust compiler. However, there are situations where this rule is not
20+
//! flexible enough. Sometimes it is required to have multiple references to an object and yet
21+
//! mutate it.
22+
//!
23+
//! Shareable mutable containers exist to permit mutability in a controlled manner, even in the
24+
//! presence of aliasing. Both `Cell<T>` and `RefCell<T>` allows to do this in a single threaded
25+
//! way. However, neither `Cell<T>` nor `RefCell<T>` are thread safe (they do not implement
26+
//! `Sync`). If you need to do aliasing and mutation between multiple threads it is possible to
27+
//! use [`Mutex`](../../std/sync/struct.Mutex.html),
28+
//! [`RwLock`](../../std/sync/struct.RwLock.html) or
29+
//! [`atomic`](../../core/sync/atomic/index.html) types.
30+
//!
1331
//! Values of the `Cell<T>` and `RefCell<T>` types may be mutated through shared references (i.e.
1432
//! the common `&T` type), whereas most Rust types can only be mutated through unique (`&mut T`)
1533
//! references. We say that `Cell<T>` and `RefCell<T>` provide 'interior mutability', in contrast

0 commit comments

Comments
 (0)