Weak references and cycles
Quick answer
Weak<T> refers to a reference-counted allocation without keeping its value alive. Use weak references for non-owning relationships, such as a child's link back to its parent.
Upgrade and ownership cycles
Rc::downgrade creates a weak reference; upgrade returns an Option containing an Rc when a strong owner still exists. Strong cycles can prevent counts from reaching zero even when no external owner remains. A weak edge can break the ownership cycle. The allocation bookkeeping may survive after the value drops.
use std::rc::Rc;fn main() { let owner = Rc::new(9); let link = Rc::downgrade(&owner); println!("{}", link.upgrade().is_some()); drop(owner); println!("{}", link.upgrade().is_some()); }Expected stdout (not executed):
truefalseUpgrade is conditional, not a permanent guarantee.
use std::rc::{Rc, Weak};struct Child { parent: Weak<String> }fn main() { let parent = Rc::new(String::from("root")); let child = Child { parent: Rc::downgrade(&parent) }; if let Some(p) = child.parent.upgrade() { println!("{p}"); } println!("owners: {}", Rc::strong_count(&parent)); }Expected stdout (not executed):
rootowners: 1The temporary upgraded owner ends before the count is printed.
Common mistake
Weak::upgrade can return None; avoid assuming the parent is alive.
Try it yourself
Draw a parent owning its child strongly and a child referring to its parent weakly.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Weak references and cycles — practice quiz
Choose one answer per question. Code examples are predictions; review explanations after attempting them.
A child owns a strong Rc to its parent, which owns the child. Which change can break the strong cycle?
Weak references and cycles — example 1. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust use std::rc::Rc; fn main() { let owner = Rc::new(9); let link = Rc::downgrade(&owner); println!("{}", link.upgrade().is_some()); drop(owner); println!("{}", link.upgrade().is_some()); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.
Weak references and cycles — example 2. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust use std::rc::{Rc, Weak}; struct Child { parent: Weak<String> } fn main() { let parent = Rc::new(String::from("root")); let child = Child { parent: Rc::downgrade(&parent) }; if let Some(p) = child.parent.upgrade() { println!("{p}"); } println!("owners: {}", Rc::strong_count(&parent)); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.