Skip to content
/ rust Public
forked from rust-lang/rust

Commit 7852ee9

Browse files
authored
Rollup merge of rust-lang#121213 - Takashiidobe:takashi/example-for-rc-into-inner, r=cuviper
Add an example to demonstrate how Rc::into_inner works This PR adds an example to Rc::into_inner, since it didn't have one previously.
2 parents 7fb3b5e + 1f2db3d commit 7852ee9

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

library/alloc/src/rc.rs

+15
Original file line numberDiff line numberDiff line change
@@ -944,6 +944,21 @@ impl<T, A: Allocator> Rc<T, A> {
944944
/// is in fact equivalent to <code>[Rc::try_unwrap]\(this).[ok][Result::ok]()</code>.
945945
/// (Note that the same kind of equivalence does **not** hold true for
946946
/// [`Arc`](crate::sync::Arc), due to race conditions that do not apply to `Rc`!)
947+
///
948+
/// # Examples
949+
///
950+
/// ```
951+
/// use std::rc::Rc;
952+
///
953+
/// let x = Rc::new(3);
954+
/// assert_eq!(Rc::into_inner(x), Some(3));
955+
///
956+
/// let x = Rc::new(4);
957+
/// let y = Rc::clone(&x);
958+
///
959+
/// assert_eq!(Rc::into_inner(y), None);
960+
/// assert_eq!(Rc::into_inner(x), Some(4));
961+
/// ```
947962
#[inline]
948963
#[stable(feature = "rc_into_inner", since = "1.70.0")]
949964
pub fn into_inner(this: Self) -> Option<T> {

0 commit comments

Comments
 (0)