std-type

LazyLock: initialize on first access

Tie an initializer to a shared lazy value. Dereferencing or forcing it triggers initialization when needed. If the initializer panics, the LazyLock is poisoned; use OnceLock if retryable initialization is required. Expected stdout (not executed): loaded 6 Common mistake: Expecting LazyLock to retry a panicking initializer automatically.

Formal Syntax & Structure

rust syntaxrust
LazyLock::new(F); LazyLock::force(&value)

Working Example

Example Snippetrust
fn main() {    use std::sync::LazyLock;    let value = LazyLock::new(|| String::from("loaded"));    println!("{}", LazyLock::force(&value));    println!("{}", value.len());}
Rust Reference