Skip to content

Archive

Initialization

1 articles
Rust 07 Sep 2026 10 min read

Initialize Shared Rust State Once with OnceLock

Applications often need one shared value that is expensive or awkward to construct but should not change after initialization. Examples include parsed configuration, a lookup table, a compiled matcher, or metadata discovered during startup. A plain static works only when the value can be created as a constant. A Mutex<Option<T>> can represent “not initialized yet,” but it also introduces a lock and a mutable state model that the program may not need after setup.