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

#249 in Procedural macros

Download history 8/week @ 2025-02-02 62/week @ 2025-02-09 266/week @ 2025-02-16 7416/week @ 2025-02-23 15603/week @ 2025-03-02 26665/week @ 2025-03-09 35285/week @ 2025-03-16 33449/week @ 2025-03-23 37765/week @ 2025-03-30 46350/week @ 2025-04-06 48826/week @ 2025-04-13 63984/week @ 2025-04-20 64317/week @ 2025-04-27 72594/week @ 2025-05-04 76494/week @ 2025-05-11 65810/week @ 2025-05-18

283,936 downloads per month
Used in 160 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