Skip to main content

static_init

Macro static_init 

Source
macro_rules! static_init {
    ($name:ident,$out:ty,$docs:expr,$($init:tt)+) => { ... };
    ($name:ident,$out:ty,$($init:tt)+) => { ... };
}
Expand description

Creates a static lazy initialization variable function.

static_init!(get_data, String, {
    "This could be an expensive operation!".to_string()
});

expands to:

pub fn get_data() -> &'static String {
    static VARBL: std::sync::OnceLock<String> = std::sync::OnceLock::new();
    VARBL.get_or_init(|| {
        "This could be an expensive operation!".to_string()
    })
}