6 releases

0.0.6 Apr 21, 2025
0.0.5 Mar 9, 2025
0.0.4 Feb 26, 2025
0.0.0 Feb 27, 2024

#243 in Procedural macros

Download history 1/week @ 2025-01-25 8/week @ 2025-02-01 11/week @ 2025-02-08 296/week @ 2025-02-15 5550/week @ 2025-02-22 16132/week @ 2025-03-01 25451/week @ 2025-03-08 34788/week @ 2025-03-15 33304/week @ 2025-03-22 38102/week @ 2025-03-29 45263/week @ 2025-04-05 48891/week @ 2025-04-12 63162/week @ 2025-04-19 63186/week @ 2025-04-26

227,888 downloads per month
Used in 146 crates (2 directly)

Apache-2.0 OR MIT

32KB
477 lines

dtor

Build Status

ctor docs.rs crates.io

dtor docs.rs crates.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);
    }

Dependencies