Cell<T>
In this lesson, you will learn about Cell<T>.
What is Cell<T>
?
It is a shareable mutable container which means shared references can be mutable. The references rules do not permit this, but there are situations wherein we need multiple mutable references. A good example would be a Doubly Linked List
wherein we need two mutable references to each node in the list.
What is interior mutability?
Interior mutability means enabling mutation inside an immutable struct. Cell<T>
implements interior mutability, here’s how Cell<T>
implements it:
pub fn replace(&self, val: T) -> T
The replace
function takes an immutable reference to Cell<T>
types and a value T
and replaces the contained value inside the Cell<T>
.
How is this possible?
This can be slightly confusing because we are taking a shared ...
Access this course and 1400+ top-rated courses and projects.