Skip to content

Commit

Permalink
Rollup merge of #81069 - ogoffart:rc_new_cyclic_doc, r=Mark-Simulacrum
Browse files Browse the repository at this point in the history
Add sample code for Rc::new_cyclic
  • Loading branch information
m-ou-se authored Jan 16, 2021
2 parents 82a66d6 + 9952632 commit a481a02
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions library/alloc/src/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,26 @@ impl<T> Rc<T> {
/// to upgrade the weak reference before this function returns will result
/// in a `None` value. However, the weak reference may be cloned freely and
/// stored for use at a later time.
///
/// # Examples
///
/// ```
/// #![feature(arc_new_cyclic)]
/// #![allow(dead_code)]
/// use std::rc::{Rc, Weak};
///
/// struct Gadget {
/// self_weak: Weak<Self>,
/// // ... more fields
/// }
/// impl Gadget {
/// pub fn new() -> Rc<Self> {
/// Rc::new_cyclic(|self_weak| {
/// Gadget { self_weak: self_weak.clone(), /* ... */ }
/// })
/// }
/// }
/// ```
#[unstable(feature = "arc_new_cyclic", issue = "75861")]
pub fn new_cyclic(data_fn: impl FnOnce(&Weak<T>) -> T) -> Rc<T> {
// Construct the inner in the "uninitialized" state with a single
Expand Down

0 comments on commit a481a02

Please sign in to comment.