std-function

Rust mem::forget(): skip destruction

forget consumes a value without executing its destructor. This is safe in Rust’s memory-safety sense but can leak resources. Do not use it as ordinary cleanup. This example forgets a zero-sized logging object, not a real allocation or external handle. Expected output (not executed): Finished Why: No destructor message appears because the value is forgotten. Common mistake: Assuming Rust guarantees every destructor runs or using forget to retain a usable pointer.

Formal Syntax & Structure

rust syntaxrust
pub const fn forget<T>(t: T)

Working Example

Example Snippetrust
use std::mem;struct Marker;impl Drop for Marker {    fn drop(&mut self) { println!("Dropped"); }}fn main() {    let marker = Marker;    mem::forget(marker);    println!("Finished");}
Rust Reference