diff options
| author | Mara Bos <m-ou.se@m-ou.se> | 2021-01-16 17:30:15 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-01-16 17:30:15 +0000 |
| commit | dd86fc62280bd3f7c88826517cf60ae034157ee3 (patch) | |
| tree | 4306407621b93d4538bd99344962cda295c2111f | |
| parent | 61be4e802471409b0f14f0ed2cf160ce446a445f (diff) | |
| parent | 9952632a2f6fc0d5c41976070ba39f57be5ede0c (diff) | |
| download | rust-dd86fc62280bd3f7c88826517cf60ae034157ee3.tar.gz rust-dd86fc62280bd3f7c88826517cf60ae034157ee3.zip | |
Rollup merge of #81069 - ogoffart:rc_new_cyclic_doc, r=Mark-Simulacrum
Add sample code for Rc::new_cyclic
| -rw-r--r-- | library/alloc/src/rc.rs | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/library/alloc/src/rc.rs b/library/alloc/src/rc.rs index 0e3bab6b20a..ee03f15eece 100644 --- a/library/alloc/src/rc.rs +++ b/library/alloc/src/rc.rs @@ -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 |
