Skip to main content

Crate dtor

Crate dtor 

Source
Expand description

§dtor

Build Status

cratedocsversion
ctordocs.rscrates.io
dtordocs.rscrates.io
link-sectiondocs.rscrates.io

Module teardown functions for Rust (like __attribute__((destructor)) in C/C++) for Linux, OSX, FreeBSD, NetBSD, Illumos, OpenBSD, DragonFlyBSD, Android, iOS, WASM, and Windows.

§Examples

Print a message at shutdown time. Note that Rust may have shut down some stdlib services at this time.

#[dtor]
unsafe fn shutdown() {
    // Using println or eprintln here will panic as Rust has shut down
    libc::printf("Shutting down!\n\0".as_ptr() as *const i8);
}

§Under the Hood

The #[dtor] macro effectively creates a constructor that calls libc::atexit with the provided function, ie roughly equivalent to:

#[ctor]
fn dtor_atexit() {
    libc::atexit(dtor);
}

Modules§

declarative
Declarative forms of the #[dtor] macro.
features
Crate features

Macros§

__features_expand_all
Expands all of the crate features into the features list.

Attribute Macros§

dtor
Marks a function as a library/executable destructor. This uses OS-specific linker sections to call a specific function at termination time.