Struct std::cell::RefCell

1.0.0 · source ·
pub struct RefCell<T>where
    T: ?Sized,
{ /* private fields */ }
Expand description

A mutable memory location with dynamically checked borrow rules

See the module-level documentation for more.

Implementations§

Creates a new RefCell containing value.

Examples
use std::cell::RefCell;

let c = RefCell::new(5);
Run

Consumes the RefCell, returning the wrapped value.

Examples
use std::cell::RefCell;

let c = RefCell::new(5);

let five = c.into_inner();
Run

Replaces 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 std::cell::RefCell;
let cell = RefCell::new(5);
let old_value = cell.replace(6);
assert_eq!(old_value, 5);
assert_eq!(cell, RefCell::new(6));
Run

Replaces 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 std::cell::RefCell;
let cell = RefCell::new(5);
let old_value = cell.replace_with(|&mut old| old + 1);
assert_eq!(old_value, 5);
assert_eq!(cell, RefCell::new(6));
Run

Swaps the wrapped value of self with the wrapped value of other, without deinitializing either one.

This function corresponds to std::mem::swap.

Panics

Panics if the value in either RefCell is currently borrowed.

Examples
use std::cell::RefCell;
let c = RefCell::new(5);
let d = RefCell::new(6);
c.swap(&d);
assert_eq!(c, RefCell::new(6));
assert_eq!(d, RefCell::new(5));
Run

Immutably borrows the wrapped value.

The borrow lasts until the returned Ref exits 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 std::cell::RefCell;

let c = RefCell::new(5);

let borrowed_five = c.borrow();
let borrowed_five2 = c.borrow();
Run

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 panic
Run

Immutably borrows the wrapped value, returning an error if the value is currently mutably borrowed.

The borrow lasts until the returned Ref exits scope. Multiple immutable borrows can be taken out at the same time.

This is the non-panicking variant of borrow.

Examples
use std::cell::RefCell;

let c = RefCell::new(5);

{
    let m = c.borrow_mut();
    assert!(c.try_borrow().is_err());
}

{
    let m = c.borrow();
    assert!(c.try_borrow().is_ok());
}
Run

Mutably borrows the wrapped value.

The borrow lasts until the returned RefMut or all RefMuts 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 std::cell::RefCell;

let c = RefCell::new("hello".to_owned());

*c.borrow_mut() = "bonjour".to_owned();

assert_eq!(&*c.borrow(), "bonjour");
Run

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 panic
Run

Mutably borrows the wrapped value, returning an error if the value is currently borrowed.

The borrow lasts until the returned RefMut or all RefMuts 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 std::cell::RefCell;

let c = RefCell::new(5);

{
    let m = c.borrow();
    assert!(c.try_borrow_mut().is_err());
}

assert!(c.try_borrow_mut().is_ok());
Run

Returns a raw pointer to the underlying data in this cell.

Examples
use std::cell::RefCell;

let c = RefCell::new(5);

let ptr = c.as_ptr();
Run

Returns a mutable reference to the underlying data.

Since this method borrows RefCell mutably, it is statically guaranteed that no borrows to the underlying data exist. The dynamic checks inherent in borrow_mut and most other methods of RefCell are therefore unnecessary.

This method can only be called if RefCell can be mutably borrowed, which in general is only the case directly after the RefCell has been created. In these situations, skipping the aforementioned dynamic borrowing checks may yield better ergonomics and runtime-performance.

In most situations where RefCell is used, it can’t be borrowed mutably. Use borrow_mut to get mutable access to the underlying data then.

Examples
use std::cell::RefCell;

let mut c = RefCell::new(5);
*c.get_mut() += 1;

assert_eq!(c, RefCell::new(6));
Run
🔬This is a nightly-only experimental API. (cell_leak #69099)

Undo the effect of leaked guards on the borrow state of the RefCell.

This call is similar to get_mut but more specialized. It borrows RefCell mutably to ensure no borrows exist and then resets the state tracking shared borrows. This is relevant if some Ref or RefMut borrows have been leaked.

Examples
#![feature(cell_leak)]
use std::cell::RefCell;

let mut c = RefCell::new(0);
std::mem::forget(c.borrow_mut());

assert!(c.try_borrow().is_err());
c.undo_leak();
assert!(c.try_borrow().is_ok());
Run

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 a Ref, thus leaving the borrow flag untouched. Mutably borrowing the RefCell while the reference returned by this method is alive is undefined behaviour.

Examples
use std::cell::RefCell;

let c = RefCell::new(5);

{
    let m = c.borrow_mut();
    assert!(unsafe { c.try_borrow_unguarded() }.is_err());
}

{
    let m = c.borrow();
    assert!(unsafe { c.try_borrow_unguarded() }.is_ok());
}
Run

Takes the wrapped value, leaving Default::default() in its place.

Panics

Panics if the value is currently borrowed.

Examples
use std::cell::RefCell;

let c = RefCell::new(5);
let five = c.take();

assert_eq!(five, 5);
assert_eq!(c.into_inner(), 0);
Run

Trait Implementations§

Panics

Panics if the value is currently mutably borrowed.

Panics

Panics if other is currently mutably borrowed.

Formats the value using the given formatter. Read more

Creates a RefCell<T>, with the Default value for T.

Creates a new RefCell<T> containing the given value.

Panics

Panics if the value in either RefCell is currently borrowed.

Panics

Panics if the value in either RefCell is currently borrowed.

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
Panics

Panics if the value in either RefCell is currently borrowed.

Panics

Panics if the value in either RefCell is currently borrowed.

Panics

Panics if the value in either RefCell is currently borrowed.

Panics

Panics if the value in either RefCell is currently borrowed.

Panics

Panics if the value in either RefCell is currently borrowed.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. 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.