Function core::ptr::invalid_mut

const: 1.61.0 · source · []
pub const fn invalid_mut<T>(addr: usize) -> *mut T
🔬 This is a nightly-only experimental API. (strict_provenance #95228)
Expand description

Creates an invalid mutable pointer with the given address.

This is currently equivalent to addr as *mut T but it expresses the intended semantic more clearly, and may become important under future memory models.

The module’s top-level documentation discusses the precise meaning of an “invalid” pointer but essentially this expresses that the pointer is not associated with any actual allocation and is little more than a usize address in disguise.

This pointer will have no provenance associated with it and is therefore UB to read/write/offset. This mostly exists to facilitate things like ptr::null and NonNull::dangling which make invalid pointers.

(Standard “Zero-Sized-Types get to cheat and lie” caveats apply, although it may be desirable to give them their own API just to make that 100% clear.)

This API and its claimed semantics are part of the Strict Provenance experiment, see the module documentation for details.