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>
impl<T> Rc<T>
Sourcepub fn try_unwrap(rc: Rc<T>) -> Result<T, Rc<T>>
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>
impl<T> Rc<T>
Sourcepub fn downgrade(&self) -> Weak<T>
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();
Sourcepub fn weak_count(this: &Rc<T>) -> usize
pub fn weak_count(this: &Rc<T>) -> usize
Get the number of weak references to this value.
Sourcepub fn strong_count(this: &Rc<T>) -> usize
pub fn strong_count(this: &Rc<T>) -> usize
Get the number of strong references to this value.
Sourcepub fn is_unique(rc: &Rc<T>) -> bool
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));
Sourcepub fn get_mut(rc: &mut Rc<T>) -> Option<&mut T>
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>
impl<T: Clone> Rc<T>
Sourcepub fn make_unique(&mut self) -> &mut T
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>
impl<T> Clone for Rc<T>
Source§fn clone(&self) -> Rc<T>
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)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl<T> Drop for Rc<T>
impl<T> Drop for Rc<T>
Source§fn drop(&mut self)
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,
drop
s 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: Ord> Ord for Rc<T>
impl<T: Ord> Ord for Rc<T>
Source§fn cmp(&self, other: &Rc<T>) -> Ordering
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) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl<T: PartialEq> PartialEq for Rc<T>
impl<T: PartialEq> PartialEq for Rc<T>
Source§impl<T: PartialOrd> PartialOrd for Rc<T>
impl<T: PartialOrd> PartialOrd for Rc<T>
Source§fn partial_cmp(&self, other: &Rc<T>) -> Option<Ordering>
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
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
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);