std-function

Release raw allocated storage

Deallocation ends the storage lifetime; it does not call the destructor of an object placed there. Pass the original allocation pointer and matching layout to the same allocator. Here no value is ever stored, so no destructor is required. Expected stdout (not executed): released Common mistake: Freeing the same pointer twice or releasing an interior pointer.

Formal Syntax & Structure

rust syntaxrust
unsafe { std::alloc::dealloc(ptr, layout) }

Working Example

Example Snippetrust
#![deny(unsafe_op_in_unsafe_fn)]fn main() {    use std::alloc::{alloc, dealloc, handle_alloc_error, Layout};    let layout = Layout::from_size_align(16, 8).unwrap();    // SAFETY: nonzero validated layout.    let ptr = unsafe { alloc(layout) };    if ptr.is_null() { handle_alloc_error(layout); }    // SAFETY: same allocator, original live pointer and identical layout, no objects to drop.    unsafe { dealloc(ptr, layout); }    println!("released");}
Rust Reference