pub struct Cell<T>where
T: ?Sized,{ /* private fields */ }
Expand description
A mutable memory location.
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;
my_struct.special_field.set(new_value);
assert_eq!(my_struct.special_field.get(), new_value);
Run
See the module-level documentation for more.
Creates a new Cell
containing the given value.
use std::cell::Cell;
let c = Cell::new(5);
Run
Sets the contained value.
use std::cell::Cell;
let c = Cell::new(5);
c.set(10);
Run
Swaps the values of two Cell
s.
Difference with std::mem::swap
is that this function doesn’t require &mut
reference.
use std::cell::Cell;
let c1 = Cell::new(5i32);
let c2 = Cell::new(10i32);
c1.swap(&c2);
assert_eq!(10, c1.get());
assert_eq!(5, c2.get());
Run
Replaces the contained value with val
, and returns the old contained value.
use std::cell::Cell;
let cell = Cell::new(5);
assert_eq!(cell.get(), 5);
assert_eq!(cell.replace(10), 5);
assert_eq!(cell.get(), 10);
Run
Unwraps the value.
use std::cell::Cell;
let c = Cell::new(5);
let five = c.into_inner();
assert_eq!(five, 5);
Run
Returns a copy of the contained value.
use std::cell::Cell;
let c = Cell::new(5);
let five = c.get();
Run
🔬This is a nightly-only experimental API. (cell_update
#50186)
Updates the contained value using a function and returns the new value.
#![feature(cell_update)]
use std::cell::Cell;
let c = Cell::new(5);
let new = c.update(|x| x + 1);
assert_eq!(new, 6);
assert_eq!(c.get(), 6);
Run
1.12.0 (const: 1.32.0) · source
Returns a raw pointer to the underlying data in this cell.
use std::cell::Cell;
let c = Cell::new(5);
let ptr = c.as_ptr();
Run
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.
use std::cell::Cell;
let mut c = Cell::new(5);
*c.get_mut() += 1;
assert_eq!(c.get(), 6);
Run
Returns a &Cell<T>
from a &mut T
use std::cell::Cell;
let slice: &mut [i32] = &mut [1, 2, 3];
let cell_slice: &Cell<[i32]> = Cell::from_mut(slice);
let slice_cell: &[Cell<i32>] = cell_slice.as_slice_of_cells();
assert_eq!(slice_cell.len(), 3);
Run
Takes the value of the cell, leaving Default::default()
in its place.
use std::cell::Cell;
let c = Cell::new(5);
let five = c.take();
assert_eq!(five, 5);
assert_eq!(c.into_inner(), 0);
Run
Returns a &[Cell<T>]
from a &Cell<[T]>
use std::cell::Cell;
let slice: &mut [i32] = &mut [1, 2, 3];
let cell_slice: &Cell<[i32]> = Cell::from_mut(slice);
let slice_cell: &[Cell<i32>] = cell_slice.as_slice_of_cells();
assert_eq!(slice_cell.len(), 3);
Run
🔬This is a nightly-only experimental API. (as_array_of_cells
#88248)
Returns a &[Cell<T>; N]
from a &Cell<[T; N]>
#![feature(as_array_of_cells)]
use std::cell::Cell;
let mut array: [i32; 3] = [1, 2, 3];
let cell_array: &Cell<[i32; 3]> = Cell::from_mut(&mut array);
let array_cell: &[Cell<i32>; 3] = cell_array.as_array_of_cells();
Run
Performs copy-assignment from
source
.
Read more
Formats the value using the given formatter.
Read more
Creates a Cell<T>
, with the Default
value for T.
Creates a new Cell<T>
containing the given value.
Compares and returns the maximum of two values.
Read more
Compares and returns the minimum of two values.
Read more
Restrict a value to a certain interval.
Read more
This method tests for
self
and
other
values to be equal, and is used
by
==
.
Read more
This method tests for
!=
. The default implementation is almost always
sufficient, and should not be overridden without very good reason.
Read more
This method returns an ordering between
self
and
other
values if one exists.
Read more
This method tests less than (for
self
and
other
) and is used by the
<
operator.
Read more
This method tests less than or equal to (for
self
and
other
) and is used by the
<=
operator.
Read more
This method tests greater than (for
self
and
other
) and is used by the
>
operator.
Read more
This method tests greater than or equal to (for
self
and
other
) and is used by the
>=
operator.
Read more
Immutably borrows from an owned value.
Read more
Mutably borrows from an owned value.
Read more
Converts to this type from the input type.
Returns the argument unchanged.
Calls U::from(self)
.
That is, this conversion is whatever the implementation of
From<T> for U
chooses to do.
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning.
Read more
Uses borrowed data to replace owned data, usually by cloning.
Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.