Macro startup::on_startup[][src]

macro_rules! on_startup {
    ($($tokens:tt)*) => { ... };
}

Run some code automatically before the execution of main.

Example

startup::on_startup! {
    println!("I'm running before main");
}
fn main() {
    println!("I'm inside main");
}

This outputs:

I'm running before main.
I'm inside main.

Caveats

  • If your program is loaded as a dynamic library via dlopen/LoadLibrary, it's not actually run "before main", but instead "when dlopen is called". In practice, this doesn't matter for most use cases.

  • This is on a best effort basis. There are known rustc bugs that will prevent it from working. There are known platforms that don't support it (wasm, maybe others). It is very important that your programs safety not rely on this being called.

  • The order two different on_startup invocations run in is totally unspecified. Different platforms do wildly different things here. Do not rely on one particular order. See also C++'s "static initialization order fiasco" (or problem)

  • Not all of the rust stdlib may be supported before main. It's best not to call into it unless you're certain it will work.