pub struct Static<T: Sync> { /* private fields */ }Expand description
A static variable intended to be initialized before main.
§Expected usage
This type is designed for the “startup is single-threaded” model: it is expected that no other threads access the value until initialization has completed. After that point, the value is treated as initialized and immutable.
§Concurrency
If the value is accessed concurrently while it is still being initialized (for example, from another thread during startup, including when used from a dynamic library), this is not undefined behavior, but this implementation does not wait for initialization to complete: it will panic instead.
If you need concurrent first access to be handled by blocking/spinning
rather than panicking, use std::sync::OnceLock or
std::sync::LazyLock (when std is available).
§Panics / poisoning
If the initializer panics, the static becomes permanently poisoned: all subsequent accesses will panic, and it cannot be reset.
Trait Implementations§
impl<T: Sync> Sync for Static<T>
SAFETY: This is safe because the static variable is either initialized and read-only, poisoned and panicing, or initializing (and will panic if multiple threads try to initialize it at the same time).