once_cell
A macroless alternative to lazy_static
.
If you like this, you might also like lazycell
If you want slightly sweeter syntax, we have macros as well!
static HASHMAP: = sync_lazy! ;
A macroless alternative to lazy_static
.
If you like this, you might also like lazycell
fn hashmap() -> &'static HashMap<u32, &'static str> {
static INSTANCE: OnceCell<HashMap<u32, &'static str>> = OnceCell::INIT;
INSTANCE.get_or_init(|| {
let mut m = HashMap::new();
m.insert(0, "foo");
m.insert(1, "bar");
m.insert(2, "baz");
m
})
}
If you want slightly sweeter syntax, we have macros as well!
static HASHMAP: Lazy<HashMap<u32, &'static str>> = sync_lazy! {
let mut m = HashMap::new();
m.insert(0, "foo");
m.insert(1, "bar");
m.insert(2, "baz");
m
};