Attribute Macro static_init::destructor

source ·
#[destructor]
Expand description

Attribute for functions run at program termination (after main)

#[destructor]
extern "C" fn droper () {
// run after main return
}

The execution order of destructors is unspecified. Nevertheless on ELF plateform (linux,any unixes but mac) and windows plateform a priority can be specified using the syntax destructor(<num>) where <num> is a number included in the range [0 ; 216-1].

Destructors with priority 0 are run first (in unspecified order), then destructors with priority number 1,… finaly destructors with priority 65535 are run.

An abscence of priority is equivalent to a priority of 0.

#[destructor(1)]
extern "C" fn first () {
// run after main return
}

#[destructor(0)]
extern "C" fn then () {
// run after main return
}

§About rust runtime

After main exit the standard streams are not buffered.

§Destructor signature

Destructor function should have type unsafe extern "C" fn() -> ().