Struct Rc

Source
pub struct Rc<T> { /* private fields */ }
Expand description

A reference-counted pointer type over an immutable value.

See the module level documentation for more details.

Implementations§

Source§

impl<T> Rc<T>

Source

pub fn new(value: T) -> Rc<T>

Constructs a new Rc<T>.

§Examples
use rc::Rc;

let five = Rc::new(5);
Source

pub fn try_unwrap(rc: Rc<T>) -> Result<T, Rc<T>>

Unwraps the contained value if the Rc<T> is unique.

If the Rc<T> is not unique, an Err is returned with the same Rc<T>.

§Examples
use rc::Rc;

let x = Rc::new(3);
assert_eq!(Rc::try_unwrap(x), Ok(3));

let x = Rc::new(4);
let _y = x.clone();
assert_eq!(Rc::try_unwrap(x), Err(Rc::new(4)));
Source§

impl<T> Rc<T>

Source

pub fn downgrade(&self) -> Weak<T>

Downgrades the Rc<T> to a Weak<T> reference.

§Examples
use rc::Rc;

let five = Rc::new(5);

let weak_five = five.downgrade();
Source

pub fn weak_count(this: &Rc<T>) -> usize

Get the number of weak references to this value.

Source

pub fn strong_count(this: &Rc<T>) -> usize

Get the number of strong references to this value.

Source

pub fn is_unique(rc: &Rc<T>) -> bool

Returns true if there are no other Rc or Weak<T> values that share the same inner value.

§Examples
use rc::Rc;

let five = Rc::new(5);

assert!(Rc::is_unique(&five));
Source

pub fn get_mut(rc: &mut Rc<T>) -> Option<&mut T>

Returns a mutable reference to the contained value if the Rc<T> is unique.

Returns None if the Rc<T> is not unique.

§Examples
use rc::Rc;

let mut x = Rc::new(3);
*Rc::get_mut(&mut x).unwrap() = 4;
assert_eq!(*x, 4);

let _y = x.clone();
assert!(Rc::get_mut(&mut x).is_none());
Source§

impl<T: Clone> Rc<T>

Source

pub fn make_unique(&mut self) -> &mut T

Make a mutable reference from the given Rc<T>.

This is also referred to as a copy-on-write operation because the inner data is cloned if the reference count is greater than one.

§Examples
use rc::Rc;

let mut five = Rc::new(5);

let mut_five = five.make_unique();

Trait Implementations§

Source§

impl<T> Clone for Rc<T>

Source§

fn clone(&self) -> Rc<T>

Makes a clone of the Rc<T>.

When you clone an Rc<T>, it will create another pointer to the data and increase the strong reference counter.

§Examples
use rc::Rc;

let five = Rc::new(5);

five.clone();
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for Rc<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Default> Default for Rc<T>

Source§

fn default() -> Rc<T>

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

§Examples
use rc::Rc;

let x: Rc<i32> = Default::default();
Source§

impl<T> Deref for Rc<T>

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &T

Dereferences the value.
Source§

impl<T: Display> Display for Rc<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Drop for Rc<T>

Source§

fn drop(&mut self)

Drops the Rc<T>.

This will decrement the strong reference count. If the strong reference count becomes zero and the only other references are Weak<T> ones, drops the inner value.

§Examples
use rc::Rc;

{
    let five = Rc::new(5);

    // stuff

    drop(five); // explicit drop
}
{
    let five = Rc::new(5);

    // stuff

} // implicit drop
Source§

impl<T: Hash> Hash for Rc<T>

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T: Ord> Ord for Rc<T>

Source§

fn cmp(&self, other: &Rc<T>) -> Ordering

Comparison for two Rc<T>s.

The two are compared by calling cmp() on their inner values.

§Examples
use rc::Rc;

let five = Rc::new(5);

five.partial_cmp(&Rc::new(5));
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<T: PartialEq> PartialEq for Rc<T>

Source§

fn eq(&self, other: &Rc<T>) -> bool

Equality for two Rc<T>s.

Two Rc<T>s are equal if their inner value are equal.

§Examples
use rc::Rc;

let five = Rc::new(5);

five == Rc::new(5);
Source§

fn ne(&self, other: &Rc<T>) -> bool

Inequality for two Rc<T>s.

Two Rc<T>s are unequal if their inner value are unequal.

§Examples
use rc::Rc;

let five = Rc::new(5);

five != Rc::new(5);
Source§

impl<T: PartialOrd> PartialOrd for Rc<T>

Source§

fn partial_cmp(&self, other: &Rc<T>) -> Option<Ordering>

Partial comparison for two Rc<T>s.

The two are compared by calling partial_cmp() on their inner values.

§Examples
use rc::Rc;

let five = Rc::new(5);

five.partial_cmp(&Rc::new(5));
Source§

fn lt(&self, other: &Rc<T>) -> bool

Less-than comparison for two Rc<T>s.

The two are compared by calling < on their inner values.

§Examples
use rc::Rc;

let five = Rc::new(5);

five < Rc::new(5);
Source§

fn le(&self, other: &Rc<T>) -> bool

‘Less-than or equal to’ comparison for two Rc<T>s.

The two are compared by calling <= on their inner values.

§Examples
use rc::Rc;

let five = Rc::new(5);

five <= Rc::new(5);
Source§

fn gt(&self, other: &Rc<T>) -> bool

Greater-than comparison for two Rc<T>s.

The two are compared by calling > on their inner values.

§Examples
use rc::Rc;

let five = Rc::new(5);

five > Rc::new(5);
Source§

fn ge(&self, other: &Rc<T>) -> bool

‘Greater-than or equal to’ comparison for two Rc<T>s.

The two are compared by calling >= on their inner values.

§Examples
use rc::Rc;

let five = Rc::new(5);

five >= Rc::new(5);
Source§

impl<T> Pointer for Rc<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Eq> Eq for Rc<T>

Auto Trait Implementations§

§

impl<T> Freeze for Rc<T>

§

impl<T> !RefUnwindSafe for Rc<T>

§

impl<T> !Send for Rc<T>

§

impl<T> !Sync for Rc<T>

§

impl<T> Unpin for Rc<T>

§

impl<T> !UnwindSafe for Rc<T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.