← All notes

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

Sources

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

Permanent ID: iil:note:4c5cc8v1

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.},
}

Markdown

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.

All artifact files (JSON index)

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.