Struct std::rc::Rc [] [src]

pub struct Rc<T> where T: ?Sized {
    // some fields omitted
}
1.0.0

A reference-counted pointer type over an immutable value.

See the module level documentation for more details.

Methods

impl<T> Rc<T> where T: Clone

fn make_mut(this: &mut Rc<T>) -> &mut T1.4.0

Make a mutable reference into the given Rc<T> by cloning the inner data if the Rc<T> doesn't have one strong reference and no weak references.

This is also referred to as a copy-on-write.

Examples

fn main() { use std::rc::Rc; let mut data = Rc::new(5); *Rc::make_mut(&mut data) += 1; // Won't clone anything let mut other_data = data.clone(); // Won't clone inner data *Rc::make_mut(&mut data) += 1; // Clones inner data *Rc::make_mut(&mut data) += 1; // Won't clone anything *Rc::make_mut(&mut other_data) *= 2; // Won't clone anything // Note: data and other_data now point to different numbers assert_eq!(*data, 8); assert_eq!(*other_data, 12); }
use std::rc::Rc;

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

*Rc::make_mut(&mut data) += 1;             // Won't clone anything
let mut other_data = data.clone(); // Won't clone inner data
*Rc::make_mut(&mut data) += 1;             // Clones inner data
*Rc::make_mut(&mut data) += 1;             // Won't clone anything
*Rc::make_mut(&mut other_data) *= 2;       // Won't clone anything

// Note: data and other_data now point to different numbers
assert_eq!(*data, 8);
assert_eq!(*other_data, 12);

impl<T> Rc<T> where T: ?Sized

fn downgrade(this: &Rc<T>) -> Weak<T>1.4.0

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

Examples

fn main() { use std::rc::Rc; let five = Rc::new(5); let weak_five = Rc::downgrade(&five); }
use std::rc::Rc;

let five = Rc::new(5);

let weak_five = Rc::downgrade(&five);

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

Unstable (rc_counts #28356)

: not clearly useful

Get the number of weak references to this value.

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

Unstable (rc_counts #28356)

: not clearly useful

Get the number of strong references to this value.

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

Unstable (rc_counts #28356)

: uniqueness has unclear meaning

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

Examples

#![feature(rc_counts)] fn main() { use std::rc::Rc; let five = Rc::new(5); assert!(Rc::is_unique(&five)); }
#![feature(rc_counts)]

use std::rc::Rc;

let five = Rc::new(5);

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

fn get_mut(this: &mut Rc<T>) -> Option<&mut T>1.4.0

Returns a mutable reference to the contained value if the Rc<T> has one strong reference and no weak references.

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

Examples

fn main() { use std::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()); }
use std::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());

impl<T> Rc<T>

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

Constructs a new Rc<T>.

Examples

fn main() { use std::rc::Rc; let five = Rc::new(5); }
use std::rc::Rc;

let five = Rc::new(5);

fn try_unwrap(this: Rc<T>) -> Result<T, Rc<T>>1.4.0

Unwraps the contained value if the Rc<T> has exactly one strong reference.

Otherwise, an Err is returned with the same Rc<T>.

This will succeed even if there are outstanding weak references.

Examples

fn main() { use std::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))); }
use std::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)));

fn would_unwrap(this: &Rc<T>) -> bool

Unstable (rc_would_unwrap #28356)

: just added for niche usecase

Checks if Rc::try_unwrap would return Ok.

Trait Implementations

impl<T> AsRef<T> for Rc<T> where T: ?Sized1.5.0

fn as_ref(&self) -> &T

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

fn borrow(&self) -> &T

impl<T> From<T> for Rc<T>1.6.0

fn from(t: T) -> Rc<T>

impl<T> Pointer for Rc<T> where T: ?Sized

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

impl<T> Debug for Rc<T> where T: Debug + ?Sized

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

impl<T> Display for Rc<T> where T: Display + ?Sized

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

impl<T> Hash for Rc<T> where T: Hash + ?Sized

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

fn hash_slice<H>(data: &[Self], state: &mut H) where H: Hasher1.3.0

impl<T> Ord for Rc<T> where T: Ord + ?Sized

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

fn main() { use std::rc::Rc; let five = Rc::new(5); five.partial_cmp(&Rc::new(5)); }
use std::rc::Rc;

let five = Rc::new(5);

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

impl<T> PartialOrd<Rc<T>> for Rc<T> where T: PartialOrd<T> + ?Sized

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

fn main() { use std::rc::Rc; let five = Rc::new(5); five.partial_cmp(&Rc::new(5)); }
use std::rc::Rc;

let five = Rc::new(5);

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

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

fn main() { use std::rc::Rc; let five = Rc::new(5); five < Rc::new(5); }
use std::rc::Rc;

let five = Rc::new(5);

five < Rc::new(5);

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

fn main() { use std::rc::Rc; let five = Rc::new(5); five <= Rc::new(5); }
use std::rc::Rc;

let five = Rc::new(5);

five <= Rc::new(5);

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

fn main() { use std::rc::Rc; let five = Rc::new(5); five > Rc::new(5); }
use std::rc::Rc;

let five = Rc::new(5);

five > Rc::new(5);

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

fn main() { use std::rc::Rc; let five = Rc::new(5); five >= Rc::new(5); }
use std::rc::Rc;

let five = Rc::new(5);

five >= Rc::new(5);

impl<T> Eq for Rc<T> where T: Eq + ?Sized

impl<T> PartialEq<Rc<T>> for Rc<T> where T: PartialEq<T> + ?Sized

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

fn main() { use std::rc::Rc; let five = Rc::new(5); five == Rc::new(5); }
use std::rc::Rc;

let five = Rc::new(5);

five == Rc::new(5);

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

fn main() { use std::rc::Rc; let five = Rc::new(5); five != Rc::new(5); }
use std::rc::Rc;

let five = Rc::new(5);

five != Rc::new(5);

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

fn default() -> Rc<T>

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

Examples

fn main() { use std::rc::Rc; let x: Rc<i32> = Default::default(); }
use std::rc::Rc;

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

impl<T> Clone for Rc<T> where T: ?Sized

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

fn main() { use std::rc::Rc; let five = Rc::new(5); five.clone(); }
use std::rc::Rc;

let five = Rc::new(5);

five.clone();

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

impl<T> Drop for Rc<T> where T: ?Sized

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

fn main() { use std::rc::Rc; { let five = Rc::new(5); // stuff drop(five); // explicit drop } { let five = Rc::new(5); // stuff } // implicit drop }
use std::rc::Rc;

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

    // stuff

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

    // stuff

} // implicit drop

impl<T> Deref for Rc<T> where T: ?Sized

type Target = T

fn deref(&self) -> &T

impl<T, U> CoerceUnsized<Rc<U>> for Rc<T> where U: ?Sized, T: Unsize<U> + ?Sized

impl<T> !Sync for Rc<T> where T: ?Sized

impl<T> !Send for Rc<T> where T: ?Sized

impl<T: RefUnwindSafe + ?Sized> UnwindSafe for Rc<T>1.9.0