.. /index

LazyLock

LazyLock

std::sync::LazyLock and std::cell::LazyCell, stabilized in Rust 1.80 (July 2024), replace the lazy_static and once_cell crates for lazy initialization. They are now part of the standard library — no external dependencies needed.

use std::sync::LazyLock;
use std::collections::HashMap;

static CONFIG: LazyLock<HashMap<String, String>> = LazyLock::new(|| {
    let mut m = HashMap::new();
    m.insert("host".into(), "localhost".into());
    m.insert("port".into(), "8080".into());
    m
});

Use LazyLock in new code instead of lazy_static! or once_cell::sync::Lazy. See modern-rust-features.

Linked from

Sources