Reference cycles in Rust: leaks and Weak pointers
Answer: Rust makes accidental memory leaks difficult, but not impossible. An Rc value is only cleaned up when its strong_count is 0, so a cycle of strong references can leak. Weak holds a non-owning reference and can be upgraded later.
How a cycle can leak
Rust's memory safety guarantees make leaks difficult, but not impossible.
It is still possible to accidentally create memory that is never cleaned up, known as a memory leak.
Calling Rc::clone increases the strong_count of an Rc instance.
An Rc instance is only cleaned up if its strong_count is 0.
You can prevent reference cycles by using Weak.
What Weak holds
Weak holds a non-owning reference to the managed allocation.
Call upgrade when you need access to that allocation.
upgrade returns an Option of Rc.
Because a Weak reference does not count towards ownership, it will not prevent the inner value from being dropped.
See also
Claims
- id: C1 claim: Memory safety makes leaks difficult, but not impossible. source: S1 evidence: "difficult, but not impossible"
- id: C2 claim: An Rc instance is only cleaned up if its strong_count is 0. source: S1 evidence: "only cleaned up if its strong_count is 0"
- id: C3 claim: Weak holds a non-owning reference to the managed allocation. source: S2 evidence: "non-owning reference to the managed allocation"
- id: C4 claim: upgrade on a Weak pointer returns an Option of Rc. source: S2 evidence: "returns an Option"
Sources
- [S1] Reference Cycles Can Leak Memory. The Rust Programming Language. https://doc.rust-lang.org/book/ch15-06-reference-cycles.html . License: MIT OR Apache-2.0. Accessed 2026-11-23.
- [S2] std::rc::Weak. Rust Standard Library. https://doc.rust-lang.org/std/rc/struct.Weak.html . License: MIT OR Apache-2.0. Accessed 2026-11-23.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: The Rust Programming Language, Reference Cycles Can Leak Memory; Rust Standard Library, std::rc::Weak. MIT OR Apache-2.0 (https://raw.githubusercontent.com/rust-lang/book/main/LICENSE-MIT). Copyright the Rust Project Developers. MIT OR Apache-2.0 (https://raw.githubusercontent.com/rust-lang/rust/master/COPYRIGHT). Copyright the Rust Project Developers. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: The Rust Programming Language, Reference Cycles Can Leak Memory; Rust Standard Library, std::rc::Weak. MIT OR Apache-2.0 (https://raw.githubusercontent.com/rust-lang/book/main/LICENSE-MIT). Copyright the Rust Project Developers. MIT OR Apache-2.0 (https://raw.githubusercontent.com/rust-lang/rust/master/COPYRIGHT). Copyright the Rust Project Developers.
Cite this
iinventedlight. 2026. "Reference cycles in Rust: leaks and Weak pointers." iinventedlight Library, iil:note:4c5cc8v1, version of 2026-11-23. https://iinventedlight.com/library/notes/rust-reference-cycles/. License: CC-BY-4.0.
@misc{iil_note_4c5cc8v1,
author = {{iinventedlight}},
title = {{Reference cycles in Rust: leaks and Weak pointers}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-23},
url = {https://iinventedlight.com/library/notes/rust-reference-cycles/},
note = {Permanent ID iil:note:4c5cc8v1. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- rust-reference-cycles.schema.json (schema; CC-BY-4.0)
- rust-reference-cycles.table.json (table; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/rust-reference-cycles.md.