Skip to content

Commit 375695c

Browse files
author
Jonathan Turner
authored
Rollup merge of rust-lang#35881 - matthew-piziak:rc-would-unwrap-example, r=GuillaumeGomez
add example for `Rc::would_unwrap` Part of rust-lang#29372 r? @steveklabnik
2 parents b560f5a + 5310d11 commit 375695c

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/liballoc/rc.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,23 @@ impl<T> Rc<T> {
263263
}
264264

265265
/// Checks if `Rc::try_unwrap` would return `Ok`.
266+
///
267+
/// # Examples
268+
///
269+
/// ```
270+
/// #![feature(rc_would_unwrap)]
271+
///
272+
/// use std::rc::Rc;
273+
///
274+
/// let x = Rc::new(3);
275+
/// assert!(Rc::would_unwrap(&x));
276+
/// assert_eq!(Rc::try_unwrap(x), Ok(3));
277+
///
278+
/// let x = Rc::new(4);
279+
/// let _y = x.clone();
280+
/// assert!(!Rc::would_unwrap(&x));
281+
/// assert_eq!(Rc::try_unwrap(x), Err(Rc::new(4)));
282+
/// ```
266283
#[unstable(feature = "rc_would_unwrap",
267284
reason = "just added for niche usecase",
268285
issue = "28356")]

0 commit comments

Comments
 (0)