#[repr(transparent)]pub struct Cell<T: ?Sized> { /* private fields */ }
Expand description
A mutable memory location.
Examples
In this example, you can see that Cell<T>
enables mutation inside an
immutable struct. In other words, it enables “interior mutability”.
use std::cell::Cell;
struct SomeStruct {
regular_field: u8,
special_field: Cell<u8>,
}
let my_struct = SomeStruct {
regular_field: 0,
special_field: Cell::new(1),
};
let new_value = 100;
// ERROR: `my_struct` is immutable
// my_struct.regular_field = new_value;
// WORKS: although `my_struct` is immutable, `special_field` is a `Cell`,
// which can always be mutated
my_struct.special_field.set(new_value);
assert_eq!(my_struct.special_field.get(), new_value);
RunSee the module-level documentation for more.
Implementations
sourceimpl<T> Cell<T>
impl<T> Cell<T>
sourceimpl<T: Copy> Cell<T>
impl<T: Copy> Cell<T>
sourceimpl<T: ?Sized> Cell<T>
impl<T: ?Sized> Cell<T>
1.11.0 · sourcepub fn get_mut(&mut self) -> &mut T
pub fn get_mut(&mut self) -> &mut T
Returns a mutable reference to the underlying data.
This call borrows Cell
mutably (at compile-time) which guarantees
that we possess the only reference.
However be cautious: this method expects self
to be mutable, which is
generally not the case when using a Cell
. If you require interior
mutability by reference, consider using RefCell
which provides
run-time checked mutable borrows through its borrow_mut
method.
Examples
use std::cell::Cell;
let mut c = Cell::new(5);
*c.get_mut() += 1;
assert_eq!(c.get(), 6);
RunTrait Implementations
1.10.0 · sourceimpl<T: Ord + Copy> Ord for Cell<T>
impl<T: Ord + Copy> Ord for Cell<T>
1.21.0 · sourcefn max(self, other: Self) -> Self where
Self: Sized,
fn max(self, other: Self) -> Self where
Self: Sized,
Compares and returns the maximum of two values. Read more
1.21.0 · sourcefn min(self, other: Self) -> Self where
Self: Sized,
fn min(self, other: Self) -> Self where
Self: Sized,
Compares and returns the minimum of two values. Read more
1.50.0 · sourcefn clamp(self, min: Self, max: Self) -> Self where
Self: Sized,
Self: PartialOrd,
fn clamp(self, min: Self, max: Self) -> Self where
Self: Sized,
Self: PartialOrd,
Restrict a value to a certain interval. Read more
1.10.0 · sourceimpl<T: PartialOrd + Copy> PartialOrd<Cell<T>> for Cell<T>
impl<T: PartialOrd + Copy> PartialOrd<Cell<T>> for Cell<T>
sourcefn partial_cmp(&self, other: &Cell<T>) -> Option<Ordering>
fn partial_cmp(&self, other: &Cell<T>) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
sourcefn lt(&self, other: &Cell<T>) -> bool
fn lt(&self, other: &Cell<T>) -> bool
This method tests less than (for self
and other
) and is used by the <
operator. Read more
sourcefn le(&self, other: &Cell<T>) -> bool
fn le(&self, other: &Cell<T>) -> bool
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
impl<T: CoerceUnsized<U>, U> CoerceUnsized<Cell<U>> for Cell<T>
impl<T: Eq + Copy> Eq for Cell<T>
impl<T: ?Sized> Send for Cell<T> where
T: Send,
impl<T: ?Sized> !Sync for Cell<T>
Auto Trait Implementations
impl<T> !RefUnwindSafe for Cell<T>
impl<T: ?Sized> Unpin for Cell<T> where
T: Unpin,
impl<T: ?Sized> UnwindSafe for Cell<T> where
T: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more