Skip to content

Commit dd86fc6

Browse files
authored
Rollup merge of #81069 - ogoffart:rc_new_cyclic_doc, r=Mark-Simulacrum
Add sample code for Rc::new_cyclic
2 parents 61be4e8 + 9952632 commit dd86fc6

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

library/alloc/src/rc.rs

+20
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,26 @@ impl<T> Rc<T> {
353353
/// to upgrade the weak reference before this function returns will result
354354
/// in a `None` value. However, the weak reference may be cloned freely and
355355
/// stored for use at a later time.
356+
///
357+
/// # Examples
358+
///
359+
/// ```
360+
/// #![feature(arc_new_cyclic)]
361+
/// #![allow(dead_code)]
362+
/// use std::rc::{Rc, Weak};
363+
///
364+
/// struct Gadget {
365+
/// self_weak: Weak<Self>,
366+
/// // ... more fields
367+
/// }
368+
/// impl Gadget {
369+
/// pub fn new() -> Rc<Self> {
370+
/// Rc::new_cyclic(|self_weak| {
371+
/// Gadget { self_weak: self_weak.clone(), /* ... */ }
372+
/// })
373+
/// }
374+
/// }
375+
/// ```
356376
#[unstable(feature = "arc_new_cyclic", issue = "75861")]
357377
pub fn new_cyclic(data_fn: impl FnOnce(&Weak<T>) -> T) -> Rc<T> {
358378
// Construct the inner in the "uninitialized" state with a single

0 commit comments

Comments
 (0)