فا
← BACK TO THE WIRE
N°0428Rust1 MIN2 SOURCES

Rust 1.99 Turns `Box::leak` Into an Ownership Review Trigger

Rust 1.99 updates the `Box::leak` guidance ahead of custom allocator stabilization, warning developers against reclaiming leaked allocations through later pointer round-trips.

SHARE
Rust
Rust 1.99 Turns `Box::leak` Into an Ownership Review Trigger
IMAGE: AI-GENERATED

Rust 1.99.0, released on October 1, adds an important ownership signal without changing the language: the standard-library documentation now recommends against taking memory returned by Box::leak and later deallocating it.

Box::leak converts a Box<T> into a reference whose lifetime can be extended, making it useful for deliberately permanent data such as process-wide configuration or registries. The risk appears when a program treats that reference as a temporary escape hatch, later reconstructs ownership through a raw pointer, and attempts to free the allocation.

The Rust release team says this round-trip pattern can interact badly with current and future compiler optimizations, with the concern becoming more significant as custom allocators approach stabilization. Rust 1.99 therefore recommends Box::into_raw or the newly stabilized Box::into_non_null when the allocation must eventually be reclaimed. Those APIs preserve an explicit ownership transition instead of presenting the allocation as leaked for the lifetime of a reference.

For library authors, the practical review question is no longer simply “does this compile?” It is “is this allocation intentionally permanent?” Search unsafe code and FFI adapters for Box::leak, raw-pointer reconstruction, and manual deallocation. If reclamation is part of the design, use an ownership-preserving conversion and document the allocator and deallocation contract.

A caveat matters: Rust 1.99 changes the documentation recommendation, not the language semantics. Intentional leaks remain a valid design choice; the warning is aimed at patterns that leak first and reclaim later.

TAGSRustRust 1.99Box::leakmemory safety
Grounded sources2 REFS
  1. [01]Announcing Rust 1.99.0blog.rust-lang.org ↗
  2. [02]Box in std::boxed — Rust 1.99.0 documentationdoc.rust-lang.org ↗
Read next

Get the wire in your inbox

Every new signal, straight from the generator. No noise, unsubscribe anytime.

RSS AVAILABLE · NO SPAM