#[repr(transparent)]pub struct Cell<T> where
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> Cell<T> where
T: Copy,
impl<T> Cell<T> where
T: Copy,
sourceimpl<T> Cell<T> where
T: ?Sized,
impl<T> Cell<T> where
T: ?Sized,
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 for Cell<T> where
T: Ord + Copy,
impl<T> Ord for Cell<T> where
T: Ord + Copy,
1.10.0 · sourceimpl<T> PartialOrd<Cell<T>> for Cell<T> where
T: PartialOrd<T> + Copy,
impl<T> PartialOrd<Cell<T>> for Cell<T> where
T: PartialOrd<T> + Copy,
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, U> CoerceUnsized<Cell<U>> for Cell<T> where
T: CoerceUnsized<U>,
impl<T> Eq for Cell<T> where
T: Eq + Copy,
impl<T> Send for Cell<T> where
T: Send + ?Sized,
impl<T> !Sync for Cell<T> where
T: ?Sized,
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