Skip to content

Recommend std::sync::LazyLock over other lazy static options #12895

Closed
@pitaj

Description

@pitaj

What it does

Warn on usage of the lazy_static! macro or the Lazy type from once_cell in declarations of static variables. Crates with an older MSRB or no_std creates would be excluded.

These solutions have been superceded by the nearly stabilized std::sync::LazyLock type (on track for 1.80.0).

Advantage

  • Remove dependency on the lazy_static or once_cell crate
  • Enforce convention

Drawbacks

Churn

Example

lazy_static! {
    static ref FOO: String = "foo".to_uppercase();
}
static BAR: Lazy<String> = Lazy::new(|| "BAR".to_lowercase());

Could be written as:

static FOO: LazyLock<String> = LazyLock::new(|| "foo".to_uppercase());
static BAR: LazyLock<String> = LazyLock::new(|| "BAR".to_lowercase());

Metadata

Metadata

Assignees

Labels

A-lintArea: New lints

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions