once_cell

Build Status Crates.io API reference

A macroless alternative to lazy_static. If you like this, you might also like lazycell.

rust 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 too!

rust 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 };

Note that this crate requires at least rustc 1.29 and the beta Cargo feature to enable full API. Without it, sync::Lazy works only in static contexts and requires explicit .deref_static call for dereference.