pub struct LazyLock<T, F = fn() -> T> {
once: Once,
data: UnsafeCell<Data<T, F>>,
}
Expand description
A value which is initialized on the first access.
This type is a thread-safe LazyCell
, and can be used in statics.
Since initialization may be called from multiple threads, any
dereferencing call will block the calling thread if another
initialization routine is currently running.
§Poisoning
If the initialization closure passed to LazyLock::new
panics, the lock will be poisoned.
Once the lock is poisoned, any threads that attempt to access this lock (via a dereference
or via an explicit call to force()
) will panic.
This concept is similar to that of poisoning in the std::sync::poison
module. A key
difference, however, is that poisoning in LazyLock
is unrecoverable. All future accesses of
the lock from other threads will panic, whereas a type in std::sync::poison
like
std::sync::poison::Mutex
allows recovery via PoisonError::into_inner()
.
§Examples
Initialize static variables with LazyLock
.
use std::sync::LazyLock;
// Note: static items do not call [`Drop`] on program termination, so this won't be deallocated.
// this is fine, as the OS can deallocate the terminated program faster than we can free memory
// but tools like valgrind might report "memory leaks" as it isn't obvious this is intentional.
static DEEP_THOUGHT: LazyLock<String> = LazyLock::new(|| {
// M3 Ultra takes about 16 million years in --release config
another_crate::great_question()
});
// The `String` is built, stored in the `LazyLock`, and returned as `&String`.
let _ = &*DEEP_THOUGHT;
Initialize fields with LazyLock
.
Fields§
§once: Once
§data: UnsafeCell<Data<T, F>>
Implementations§
Source§impl<T, F: FnOnce() -> T> LazyLock<T, F>
impl<T, F: FnOnce() -> T> LazyLock<T, F>
1.80.0 (const: 1.80.0) · Sourcepub const fn new(f: F) -> LazyLock<T, F>
pub const fn new(f: F) -> LazyLock<T, F>
Creates a new lazy value with the given initializing function.
§Examples
Sourcepub fn into_inner(this: Self) -> Result<T, F>
🔬This is a nightly-only experimental API. (lazy_cell_into_inner
#125623)
pub fn into_inner(this: Self) -> Result<T, F>
lazy_cell_into_inner
#125623)Sourcepub fn force_mut(this: &mut LazyLock<T, F>) -> &mut T
🔬This is a nightly-only experimental API. (lazy_get
#129333)
pub fn force_mut(this: &mut LazyLock<T, F>) -> &mut T
lazy_get
#129333)Forces the evaluation of this lazy value and returns a mutable reference to the result.
§Panics
If the initialization closure panics (the one that is passed to the new()
method), the
panic is propagated to the caller, and the lock becomes poisoned. This will cause all future
accesses of the lock (via force()
or a dereference) to panic.
§Examples
1.80.0 · Sourcepub fn force(this: &LazyLock<T, F>) -> &T
pub fn force(this: &LazyLock<T, F>) -> &T
Forces the evaluation of this lazy value and returns a reference to
result. This is equivalent to the Deref
impl, but is explicit.
This method will block the calling thread if another initialization routine is currently running.
§Panics
If the initialization closure panics (the one that is passed to the new()
method), the
panic is propagated to the caller, and the lock becomes poisoned. This will cause all future
accesses of the lock (via force()
or a dereference) to panic.
§Examples
Source§impl<T, F> LazyLock<T, F>
impl<T, F> LazyLock<T, F>
Sourcepub fn get_mut(this: &mut LazyLock<T, F>) -> Option<&mut T>
🔬This is a nightly-only experimental API. (lazy_get
#129333)
pub fn get_mut(this: &mut LazyLock<T, F>) -> Option<&mut T>
lazy_get
#129333)Returns a mutable reference to the value if initialized. Otherwise (if uninitialized or
poisoned), returns None
.
§Examples
Trait Implementations§
1.80.0 · Source§impl<T, F: FnOnce() -> T> Deref for LazyLock<T, F>
impl<T, F: FnOnce() -> T> Deref for LazyLock<T, F>
Source§fn deref(&self) -> &T
fn deref(&self) -> &T
Dereferences the value.
This method will block the calling thread if another initialization routine is currently running.
§Panics
If the initialization closure panics (the one that is passed to the new()
method), the
panic is propagated to the caller, and the lock becomes poisoned. This will cause all future
accesses of the lock (via force()
or a dereference) to panic.
impl<T: RefUnwindSafe + UnwindSafe, F: UnwindSafe> RefUnwindSafe for LazyLock<T, F>
impl<T: Sync + Send, F: Send> Sync for LazyLock<T, F>
impl<T: UnwindSafe, F: UnwindSafe> UnwindSafe for LazyLock<T, F>
Auto Trait Implementations§
impl<T, F = fn() -> T> !Freeze for LazyLock<T, F>
impl<T, F> Send for LazyLock<T, F>
impl<T, F> Unpin for LazyLock<T, F>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> SizedTypeProperties for T
impl<T> SizedTypeProperties for T
Source§#[doc(hidden)] const IS_ZST: bool = _
#[doc(hidden)] const IS_ZST: bool = _
sized_type_properties
)Source§#[doc(hidden)] const LAYOUT: Layout = _
#[doc(hidden)] const LAYOUT: Layout = _
sized_type_properties
)Source§#[doc(hidden)] const MAX_SLICE_LEN: usize = _
#[doc(hidden)] const MAX_SLICE_LEN: usize = _
sized_type_properties
)[Self]
. Read more