src/user-defined-types/static.md
staticStatic variables will live during the whole execution of the program, and therefore will not move:
# // Copyright 2024 Google LLC
# // SPDX-License-Identifier: Apache-2.0
#
static BANNER: &str = "Welcome to RustOS 3.14";
fn main() {
println!("{BANNER}");
}
As noted in the Rust RFC Book, these are not inlined upon use and have an
actual associated memory location. This is useful for unsafe and embedded code,
and the variable lives through the entirety of the program execution. When a
globally-scoped value does not have a reason to need object identity, const is
generally preferred.
static is similar to mutable global variables in C++.static provides object identity: an address in memory and state as required
by types with interior mutability such as Mutex<T>.Because static variables are accessible from any thread, they must be Sync.
Interior mutability is possible through a
Mutex, atomic or
similar.
It is common to use OnceLock in a static as a way to support initialization on
first use. OnceCell is not Sync and thus cannot be used in this context.
Thread-local data can be created with the macro std::thread_local.