Skip to main content

Crate init_static

Crate init_static 

Source
Expand description

§init_static

Crates.io Documentation

A Rust library for explicit static initialization.

§Overview

init_static provides a macro similar to lazy_static, but with explicit initialization control. This means you decide when your statics are initialized, rather than having them lazily evaluate on first use.

Unlike lazy_static, init_static uses std::sync::OnceLock internally and does not initialize your static values automatically. Instead, it gathers your initialization functions at compile-time using linkme, and provides a function to run them all at once early in your program (for example, inside main()).

§Compare to lazy_static

Featurelazy_staticinit_static
InitializationImplicit (on first use)Explicit (init_static() call)
Result supportNot supportedSupported
Async supportNot supportedSupported
Early failureNo (fail at runtime)Yes (optional, before app starts)

§Installation

Add this to your Cargo.toml:

[dependencies]
init_static = { version = "0.5" }

§Example

use init_static::init_static;

init_static! {
    static VALUE: u32 = "42".parse()?;
}

#[tokio::main]
async fn main() {
    init_static().await.unwrap();
    println!("{}", *VALUE);
}

Macros§

InitStatic
Creates a new uninitialized InitStatic<T> instance with source location metadata.
Symbol
Creates a Symbol reference capturing the current source location for the given identifier.
init_static
Macro to declare statically stored values with explicit initialization. Similar to lazy_static!, but initialization is not automatic.

Structs§

InitStatic
A wrapper around OnceLock providing safe initialization and Deref support to mimic the ergonomics of lazy_static!.
Symbol
Represents the source location and identity of a static variable declared via init_static!.

Enums§

InitError
Error type returned by init_static() when initialization fails.

Functions§

init_static
Runs initialization for all statics declared with init_static!.
is_initialized
Returns whether init_static() has already been called.
set_debug
Enables or disables debug output during initialization.