RS.CLIPPY.NON_STD_LAZY_STATICS
Lazy static that could be replaced by `std::sync::LazyLock`
This checker is a Clippy lint created by The Rust Project Contributors. The documentation shown here is a copy of the original documentation for: non_std_lazy_statics. Copyright ©2025 The Rust Team. All rights reserved.
What it does
Lints when once_cell::sync::Lazy or lazy_static! are used to define a static variable,
and suggests replacing such cases with std::sync::LazyLock instead.
Note: This lint will not trigger in crate with no_std context, or with MSRV < 1.80.0. It
also will not trigger on once_cell::sync::Lazy usage in crates which use other types
from once_cell, such as once_cell::race::OnceBox.
Why restrict this?
- Reduces the need for an extra dependency
- Enforce convention of using standard library types when possible
Example
lazy_static! {
static ref FOO: String = "foo".to_uppercase();
}
static BAR: once_cell::sync::Lazy<String> = once_cell::sync::Lazy::new(|| "BAR".to_lowercase());
Use instead:
static FOO: std::sync::LazyLock<String> = std::sync::LazyLock::new(|| "FOO".to_lowercase());
static BAR: std::sync::LazyLock<String> = std::sync::LazyLock::new(|| "BAR".to_lowercase());
Configuration
-
msrv: The minimum rust version that the project supports. Defaults to therust-versionfield inCargo.toml(default:
current version)