Struct RefCell
struct RefCell<T: ?Sized> { ... }
A mutable memory location with dynamically checked borrow rules
See the module-level documentation for more.
Implementations
impl<T> RefCell<T>
const fn new(value: T) -> RefCell<T>Creates a new
RefCellcontainingvalue.Examples
use RefCell; let c = new;const fn into_inner(self: Self) -> TConsumes the
RefCell, returning the wrapped value.Examples
use RefCell; let c = new; let five = c.into_inner;const fn replace(self: &Self, t: T) -> TReplaces the wrapped value with a new one, returning the old value, without deinitializing either one.
This function corresponds to
std::mem::replace.Panics
Panics if the value is currently borrowed.
Examples
use RefCell; let cell = new; let old_value = cell.replace; assert_eq!; assert_eq!;fn replace_with<F: FnOnce(&mut T) -> T>(self: &Self, f: F) -> TReplaces the wrapped value with a new one computed from
f, returning the old value, without deinitializing either one.Panics
Panics if the value is currently borrowed.
Examples
use RefCell; let cell = new; let old_value = cell.replace_with; assert_eq!; assert_eq!;const fn swap(self: &Self, other: &Self)Swaps the wrapped value of
selfwith the wrapped value ofother, without deinitializing either one.This function corresponds to
std::mem::swap.Panics
Panics if the value in either
RefCellis currently borrowed, or ifselfandotherpoint to the sameRefCell.Examples
use RefCell; let c = new; let d = new; c.swap; assert_eq!; assert_eq!;
impl<T: ?Sized> RefCell<T>
const fn borrow(self: &Self) -> Ref<'_, T>Immutably borrows the wrapped value.
The borrow lasts until the returned
Refexits scope. Multiple immutable borrows can be taken out at the same time.Panics
Panics if the value is currently mutably borrowed. For a non-panicking variant, use
try_borrow.Examples
use RefCell; let c = new; let borrowed_five = c.borrow; let borrowed_five2 = c.borrow;An example of panic:
use std::cell::RefCell; let c = RefCell::new(5); let m = c.borrow_mut(); let b = c.borrow(); // this causes a panicconst fn try_borrow(self: &Self) -> Result<Ref<'_, T>, BorrowError>Immutably borrows the wrapped value, returning an error if the value is currently mutably borrowed.
The borrow lasts until the returned
Refexits scope. Multiple immutable borrows can be taken out at the same time.This is the non-panicking variant of
borrow.Examples
use RefCell; let c = new;const fn borrow_mut(self: &Self) -> RefMut<'_, T>Mutably borrows the wrapped value.
The borrow lasts until the returned
RefMutor allRefMuts derived from it exit scope. The value cannot be borrowed while this borrow is active.Panics
Panics if the value is currently borrowed. For a non-panicking variant, use
try_borrow_mut.Examples
use RefCell; let c = new; *c.borrow_mut = "bonjour".to_owned; assert_eq!;An example of panic:
use std::cell::RefCell; let c = RefCell::new(5); let m = c.borrow(); let b = c.borrow_mut(); // this causes a panicconst fn try_borrow_mut(self: &Self) -> Result<RefMut<'_, T>, BorrowMutError>Mutably borrows the wrapped value, returning an error if the value is currently borrowed.
The borrow lasts until the returned
RefMutor allRefMuts derived from it exit scope. The value cannot be borrowed while this borrow is active.This is the non-panicking variant of
borrow_mut.Examples
use RefCell; let c = new; assert!;const fn as_ptr(self: &Self) -> *mut TReturns a raw pointer to the underlying data in this cell.
Examples
use RefCell; let c = new; let ptr = c.as_ptr;const fn get_mut(self: &mut Self) -> &mut TReturns a mutable reference to the underlying data.
Since this method borrows
RefCellmutably, it is statically guaranteed that no borrows to the underlying data exist. The dynamic checks inherent inborrow_mutand most other methods ofRefCellare therefore unnecessary. Note that this method does not reset the borrowing state if borrows were previously leaked (e.g., viaforget()on aReforRefMut). For that purpose, consider using the unstableundo_leakmethod.This method can only be called if
RefCellcan be mutably borrowed, which in general is only the case directly after theRefCellhas been created. In these situations, skipping the aforementioned dynamic borrowing checks may yield better ergonomics and runtime-performance.In most situations where
RefCellis used, it can't be borrowed mutably. Useborrow_mutto get mutable access to the underlying data then.Examples
use RefCell; let mut c = new; *c.get_mut += 1; assert_eq!;const fn undo_leak(self: &mut Self) -> &mut TUndo the effect of leaked guards on the borrow state of the
RefCell.This call is similar to
get_mutbut more specialized. It borrowsRefCellmutably to ensure no borrows exist and then resets the state tracking shared borrows. This is relevant if someReforRefMutborrows have been leaked.Examples
use RefCell; let mut c = new; forget; assert!; c.undo_leak; assert!;unsafe const fn try_borrow_unguarded(self: &Self) -> Result<&T, BorrowError>Immutably borrows the wrapped value, returning an error if the value is currently mutably borrowed.
Safety
Unlike
RefCell::borrow, this method is unsafe because it does not return aRef, thus leaving the borrow flag untouched. Mutably borrowing theRefCellwhile the reference returned by this method is alive is undefined behavior.Examples
use RefCell; let c = new;
impl<T: Default> RefCell<T>
fn take(self: &Self) -> TTakes the wrapped value, leaving
Default::default()in its place.Panics
Panics if the value is currently borrowed.
Examples
use RefCell; let c = new; let five = c.take; assert_eq!; assert_eq!;
impl<T> Any for RefCell<T>
fn type_id(self: &Self) -> TypeId
impl<T> Borrow for RefCell<T>
fn borrow(self: &Self) -> &T
impl<T> BorrowMut for RefCell<T>
fn borrow_mut(self: &mut Self) -> &mut T
impl<T> CloneToUninit for RefCell<T>
unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)
impl<T> Freeze for RefCell<T>
impl<T> From for RefCell<T>
fn from(t: T) -> RefCell<T>Creates a new
RefCell<T>containing the given value.
impl<T> From for RefCell<T>
fn from(t: T) -> TReturns the argument unchanged.
impl<T> From for RefCell<T>
fn from(t: never) -> T
impl<T> RefUnwindSafe for RefCell<T>
impl<T> Send for RefCell<T>
impl<T> Unpin for RefCell<T>
impl<T> UnwindSafe for RefCell<T>
impl<T, U> Into for RefCell<T>
fn into(self: Self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom for RefCell<T>
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto for RefCell<T>
fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>
impl<T: ?Sized + Debug> Debug for crate::cell::RefCell<T>
fn fmt(self: &Self, f: &mut Formatter<'_>) -> Result
impl<T: ?Sized + Eq> Eq for RefCell<T>
impl<T: ?Sized + Ord> Ord for RefCell<T>
fn cmp(self: &Self, other: &RefCell<T>) -> OrderingPanics
Panics if the value in either
RefCellis currently mutably borrowed.
impl<T: ?Sized + PartialEq> PartialEq for RefCell<T>
fn eq(self: &Self, other: &RefCell<T>) -> boolPanics
Panics if the value in either
RefCellis currently mutably borrowed.
impl<T: ?Sized + PartialOrd> PartialOrd for RefCell<T>
fn partial_cmp(self: &Self, other: &RefCell<T>) -> Option<Ordering>Panics
Panics if the value in either
RefCellis currently mutably borrowed.fn lt(self: &Self, other: &RefCell<T>) -> boolPanics
Panics if the value in either
RefCellis currently mutably borrowed.fn le(self: &Self, other: &RefCell<T>) -> boolPanics
Panics if the value in either
RefCellis currently mutably borrowed.fn gt(self: &Self, other: &RefCell<T>) -> boolPanics
Panics if the value in either
RefCellis currently mutably borrowed.fn ge(self: &Self, other: &RefCell<T>) -> boolPanics
Panics if the value in either
RefCellis currently mutably borrowed.
impl<T: ?Sized> PinCoerceUnsized for RefCell<T>
impl<T: ?Sized> Sync for RefCell<T>
impl<T: Clone> Clone for RefCell<T>
fn clone(self: &Self) -> RefCell<T>Panics
Panics if the value is currently mutably borrowed.
fn clone_from(self: &mut Self, source: &Self)Panics
Panics if
sourceis currently mutably borrowed.
impl<T: CoerceUnsized<U>, U> CoerceUnsized for RefCell<T>
impl<T: ~const Default> Default for RefCell<T>
fn default() -> RefCell<T>Creates a
RefCell<T>, with theDefaultvalue for T.