#[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.21.0 · sourceconst fn max(self, other: Self) -> Self
const fn max(self, other: Self) -> Self
Compares and returns the maximum of two values. Read more
1.21.0 · sourceconst fn min(self, other: Self) -> Self
const fn min(self, other: Self) -> Self
Compares and returns the minimum of two values. Read more
1.50.0 · sourceconst fn clamp(self, min: Self, max: Self) -> Selfwhere
Self: PartialOrd<Self>,
const fn clamp(self, min: Self, max: Self) -> Selfwhere
Self: PartialOrd<Self>,
Restrict a value to a certain interval. Read more
sourceimpl<T> PartialEq<Cell<T>> for Cell<T>where
T: PartialEq<T> + Copy,
impl<T> PartialEq<Cell<T>> for Cell<T>where
T: PartialEq<T> + 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,
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 Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
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