Rust Globals


  • const IDENTIFIER: type = constexpr;
  • static [mut] IDENTIFIER: type = expr;
  • lazy_static! { static ref IDENTIFIER: type = expr; }


  • const values are always inlined and have no address in memory.
  • static values are never inlined and have one instance with a fixed address.
  • static mut values are not memory safe and thus can only be accessed in an unsafe block.
  • Sometimes using global static mutable variables in multi-threaded code can be dangerous, so consider using std::sync::Mutex or other alternatives
  • lazy_static objects are immutable, are initialized only once, are shared among all threads, and can be directly accessed (there are no wrapper types involved). In contrast, thread_local objects are meant to be mutable, are initialized once for each thread, and accesses are indirect (involving the wrapper type LocalKey<T>)