mirror of
https://github.com/openharmony/third_party_rust_once_cell.git
synced 2026-07-01 21:14:07 -04:00
63ea1029b8fd4e67776acdfc325dbf8138dbedf7
once_cell
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
};
Description
提供一种在不使用互斥锁的情况下实现只初始化一次的单元格类型。 | A Rust library that provides a cell that can only be written to once.
Languages
Rust
100%