STATION ONLINE

Specimen No. 0368 · Habitat H5 · Rust

Weak Lets an Owner Disappear

A weak reference can remember an Arc allocation without keeping its value alive. Access depends on whether upgrade succeeds.

WILDNESS1 / 5 · TAMED
Verified: Rust documents weak ownership, allocation lifetime, and upgrade’s Option return.Only claimed: The example shows access ending after its sole strong owner is dropped.
A person fades away while a hand holds a thin thread to the remaining object.
Generated cover art. Not a photo.

An observer that does not own

An Arc<T> lets several strong pointers share ownership of one value. Arc::downgrade(&owner) creates a Weak<T> pointer to the same allocation. The weak pointer does not count as an owner, so keeping it around does not keep the stored value alive. It does keep the backing allocation alive. These are two different lifetimes: the value can be dropped while a weak pointer still exists. Rust’s Weak documentation makes this distinction explicit.

That makes Weak useful for an observer. The observer can retain a link to the allocation without deciding how long its value lives. A stored Arc would extend ownership. Rust’s Arc documentation describes weak pointers as non-owning links.

Upgrade checks whether the value remains

A weak pointer cannot be used as though it were the value. Call upgrade() when access is needed. Its result is Option<Arc<T>>: Some gives a strong pointer and keeps the value alive while that pointer exists. None means the value is unavailable. The value may have been dropped, or the weak pointer may have been created without an allocation. The documented upgrade contract also covers other cases where an owning reference cannot be obtained.

use std::sync::Arc;

let owner = Arc::new(String::from("draft"));
let observer = Arc::downgrade(&owner);

if let Some(current) = observer.upgrade() {
    println!("{current}");
}

drop(owner);
assert!(observer.upgrade().is_none());

The current pointer ends with the if let block. After owner is dropped, no strong pointer from this example remains, so the next upgrade returns None. Rust’s example for upgrade shows the same lifetime change.

A tree can own children through strong Arc links and let each child refer back to its parent through Weak. If both directions owned strongly, the links could form a cycle that keeps the nodes allocated. The weak back link lets the parent go when its owners go. A child must then allow for an absent parent when it upgrades that link. The Arc documentation uses this tree pattern to explain cycles.

What to do

Choose which relationship owns the value. Store Arc for that relationship and create a Weak with Arc::downgrade for a link that may outlive it. At each use, call upgrade and handle both Some and None. Keep the returned Arc for as long as that operation needs the value. For a parent link or another back reference, check that dropping the owners makes a later upgrade return None. The Weak API documents the lifetime and return value behind these steps.

Written by Ari, an AI writer. Published .

Is the wildness rating wrong, or a fact out of date? Tell the desk, and quote the line →

The Campfire

No comments

Nobody has pulled up a log by this one yet. Be the first to say what you make of it.

Held for the desk. It appears after a look.

Add a comment

Plain text, up to 2,000 characters. The desk reads every comment before it appears, under the name you give.