Struct std::rc::Rc
[−]
[src]
pub struct Rc<T> where T: ?Sized {
// some fields omitted
}
A reference-counted pointer type over an immutable value.
See the module level documentation for more details.
Methods
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(rc: Rc<T>) -> Result<T, Rc<T>>
rc_unique
#27718)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
#![feature(rc_unique)] 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))); }#![feature(rc_unique)] 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)));
impl<T> Rc<T> where T: ?Sized
fn downgrade(&self) -> Weak<T>
Downgrades the Rc<T>
to a Weak<T>
reference.
Examples
#![feature(rc_weak)] fn main() { use std::rc::Rc; let five = Rc::new(5); let weak_five = five.downgrade(); }#![feature(rc_weak)] use std::rc::Rc; let five = Rc::new(5); let weak_five = five.downgrade();
fn weak_count(this: &Rc<T>) -> usize
rc_counts
#27718)Get the number of weak references to this value.
fn strong_count(this: &Rc<T>) -> usize
rc_counts
#27718)Get the number of strong references to this value.
fn is_unique(rc: &Rc<T>) -> bool
rc_unique
#27718)Returns true if there are no other Rc
or Weak<T>
values that share
the same inner value.
Examples
#![feature(rc_unique)] fn main() { use std::rc::Rc; let five = Rc::new(5); assert!(Rc::is_unique(&five)); }#![feature(rc_unique)] use std::rc::Rc; let five = Rc::new(5); assert!(Rc::is_unique(&five));
fn get_mut(rc: &mut Rc<T>) -> Option<&mut T>
rc_unique
#27718)Returns a mutable reference to the contained value if the Rc<T>
is
unique.
Returns None
if the Rc<T>
is not unique.
Examples
#![feature(rc_unique)] 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()); }#![feature(rc_unique)] 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> where T: Clone
fn make_unique(&mut self) -> &mut T
rc_unique
#27718)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
#![feature(rc_unique)] fn main() { use std::rc::Rc; let mut five = Rc::new(5); let mut_five = five.make_unique(); }#![feature(rc_unique)] use std::rc::Rc; let mut five = Rc::new(5); let mut_five = five.make_unique();