12 releases

0.2.0 Jul 20, 2024
0.1.10 Jun 21, 2024

#2133 in Rust patterns


Used in http-srv

MIT license

4KB
58 lines

Lazy Init

Rust macro to declare lazily initialized static types.


lib.rs:

This is a confortable wrapper arround standard ways of declaring lazily initialized types.

Examples

Using the [DelayInit] struct

use delay_init::*;
use std::collections::HashMap;

static NUMBERS : DelayInit<HashMap<i32,i32>> = DelayInit::new(|| {
    let mut map = HashMap::new();
    map.insert(12,12);
    map.insert(13,13);
    map
});

fn main() {
    println!("{}", NUMBERS.get(&12).unwrap());
}

Using the [delay] macro

use delay_init::*;
use std::collections::HashMap;

delay! {
    static NUMBERS : HashMap<i32,i32> = {
        let mut map = HashMap::new();
        map.insert(12,12);
        map.insert(13,13);
        map
    };
}

fn main() {
    println!("{}", NUMBERS.get(&12).unwrap());
}

Dependencies

~37KB