std-type

Rust std::rc::Weak<T>

Weak is an observer of a reference-counted allocation, not an owner of its payload. The payload can disappear even while the allocation backing store remains reserved for weak handles. Upgrade before reading; it may fail. Expected output (not executed): 1 1 true Why: The observer does not add a strong owner. Common mistake: Assuming the existence of a Weak means the payload still exists.

Formal Syntax & Structure

rust syntaxrust
Rc::downgrade(&owner) -> Weak<T>

Working Example

Example Snippetrust
use std::rc::Rc;fn main() {    let profile = Rc::new(String::from("guest"));    let observer = Rc::downgrade(&profile);    println!("{} {}", Rc::strong_count(&profile), Rc::weak_count(&profile));    drop(profile);    println!("{}", observer.upgrade().is_none());}
Rust Reference