lazy_static

A macro for declaring lazily evaluated static variables in Rust

Library
Cargo
v1.5.0
2,033 stars
Apache License 2.0

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum. How we score it →
64 /100 Good
Development Activity 52
Maintenance 40
Community 64
Maturity 60
Momentum 40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation. How we score it →
79 /100 Good
Architecture 78
Code Quality 82
Innovation 85
Learning Curve 85

lazy_static is a small procedural macro crate that lets Rust programs declare static values whose initialization requires runtime code — heap allocations, function calls, or anything that isn’t a const expression. Before std::sync::LazyLock stabilized in the standard library, this was the standard way to build globally-accessible, once-initialized data such as compiled regexes, parsed configuration, or lookup tables, and it remains one of the most widely depended-upon crates in the Rust ecosystem.

Under the hood, each static ref NAME: TYPE = EXPR; declaration expands into a hidden marker type plus a Deref implementation that triggers the expression’s evaluation exactly once, on first access, backed by std::sync::Once (or the spin crate in no_std environments via the spin_no_std feature). The result behaves like an ordinary &'static TYPE reference everywhere it’s used.

What You Get

  • The lazy_static! macro for declaring one or more lazily-initialized statics in a single block, with support for visibility modifiers, doc comments, and attributes
  • Thread-safe, exactly-once initialization backed by std::sync::Once, so concurrent first access from multiple threads is race-free
  • A no_std-compatible code path (spin_no_std feature) that swaps the std::sync::Once implementation for the spin crate’s spinlock-based Once, for embedded or kernel-level targets
  • A public lazy_static::initialize() function to force eager initialization at a controlled point instead of waiting for first access
  • Zero runtime dependencies in the default configuration — pure core/std, no external crates required

Common Use Cases

  • Compiling a Regex once at first use instead of recompiling it on every function call
  • Building a static lookup table, HashMap, or Vec that requires heap allocation and can’t be expressed as a const
  • Caching the result of an expensive one-time computation (e.g. parsing an embedded config or computing a derived constant) behind a global reference
  • Sharing a single initialized instance of a type across an application without threading it through every function signature

Under The Hood

Architecture — The crate implements a macro-based lazy-initialization pattern. src/lib.rs defines the public lazy_static! macro (built on top of an internal __lazy_static_internal! macro) that generates, for each declared static, a unique marker struct implementing Deref<Target = TYPE>; the first call to deref() evaluates the user’s expression and stores it, and every subsequent call returns a reference to the already-computed value. The actual storage and once-only-execution logic lives behind a __lazy_static_create! macro whose implementation is swapped at compile time between src/inline_lazy.rs (default) and src/core_lazy.rs (the spin_no_std feature), letting the same public macro surface target both hosted std environments and no_std embedded targets.

Tech Stack — Pure Rust with a #![no_std] core crate; the only dependency is the optional spin crate (v0.9.8, default-features off, once feature) gated behind the spin_no_std feature flag. Dev-dependencies are doc-comment (to doctest the README’s code samples) and trybuild (for compile-fail/UI testing). There’s no build script and no external tooling beyond Cargo; MSRV is pinned at Rust 1.40.0 and verified in CI via a GitHub Actions workflow.

Code Quality — tests/test.rs exercises basic declarations, doc-comment/attribute passthrough, cross-referencing statics from separate lazy_static! blocks, unsafe-expression initialization, and array/box element types, all under #![deny(warnings)]. tests/no_std.rs verifies the spin_no_std path compiles and runs correctly. Notably, tests/ui.rs plus the tests/compile_fail/ fixtures use trybuild to assert that common misuse — referencing a private static from another module, incorrect visibility restrictions — fails to compile with the expected diagnostic, which is a strong quality signal for a macro crate whose main risk surface is macro-expansion misuse rather than runtime logic.

API Design — The public API is a single macro requiring only use lazy_static::lazy_static; and a static ref NAME: TYPE = EXPR; line per value — effectively zero boilerplate. Visibility, doc comments, and other attributes thread through unmodified, and the resulting static is used exactly like a normal &TYPE reference thanks to the transparent Deref implementation. The crate’s own documentation is unusually candid for a mature, maintenance-mode crate: lib.rs includes a dedicated “Standard library” section that shows the equivalent std::sync::LazyLock-based code and points new users toward the stdlib alternative where it’s available.

Used by 23 apps in this directory

Rust
65%
Apache 2.0

agentgateway

AI Development · Developer Tools · Mcp

5,071

An open source AI-native proxy that secures, observes, and governs agent-to-LLM, agent-to-tool, and agent-to-agent communication through MCP, A2A, and unified LLM routing.

View details
88
Repo Health
82
Technical
69
Dependency
Built with
Rust 65%
Go 23%
Updated 5 days ago
Dart
74%
AGPL 3.0

AppFlowy

AI Assistants · Collaboration · Productivity

76,967

The open-source AI workspace that puts your data, your rules — with local LLMs, CRDT collaboration, and full self-hosting built in.

View details
66
Repo Health
81
Technical
67
Dependency
Built with
Dart 74%
Rust 24%
Updated 1 weeks ago
Rust
54%
Other

Cap

Team Chat · Video Conferencing

22,884

Open source Loom alternative with GPU-accelerated recording, instant share links, AI summaries, and full self-hosting via Docker Compose.

View details
89
Repo Health
81
Technical
64
Dependency
Built with
Rust 54%
TypeScript 42%
Updated 4 days ago
Svelte
44%
MIT

Cryptgeon

File Storage · Security

1,538

Self-destructing encrypted notes and files that vanish after viewing — the server never sees your keys.

View details
88
Repo Health
80
Technical
74
Dependency
Built with
Svelte 44%
TypeScript 39%
Rust 13%
Updated 6 days ago
Go
32%
Apache 2.0

CubeSandbox

AI Agents · Developer Tools · Security

12,728

Instant, concurrent, hardware-isolated MicroVM sandboxes for AI agents — E2B-API compatible, sub-60ms cold starts, and a built-in zero-trust egress proxy, all self-hostable at scale.

View details
82
Repo Health
88
Technical
63
Dependency
Built with
Go 32%
Rust 26%
C 22%
Updated 1 weeks ago
TypeScript
64%
Other

Epicenter

Developer Tools · Knowledge Management · Note Taking

4,808

A local-first monorepo led by Whispering, an open-source speech-to-text app, built on an MIT toolkit that turns your data into plain Markdown and SQLite files you own instead of a database you rent.

View details
88
Repo Health
90
Technical
64
Dependency
Built with
TypeScript 64%
HTML 13%
Svelte 13%
Updated 5 days ago
Rust
65%
Other

GitButler

AI Development · Developer Tools · Devops

21,726

Git, but better — a modern version control client with stacked branches, parallel workflows, unlimited undo, and first-class support for AI-powered development.

View details
89
Repo Health
84
Technical
66
Dependency
Built with
Rust 65%
TypeScript 22%
Svelte 10%
Updated 4 days ago
Rust
89%
Apache 2.0

Graphite

Code Editors · Design Tools

27,377

A free, open source procedural 2D design tool that unifies vector, raster, motion graphics, and generative art in a single node-based nondestructive workflow.

View details
84
Repo Health
77
Technical
70
Dependency
Built with
Rust 89%
Updated 5 days ago
TypeScript
67%
MIT

Hoppscotch

Developer Tools

80,529

A lightweight, offline-capable API development ecosystem for testing HTTP, GraphQL, WebSocket, MQTT, and SSE endpoints across web, desktop, and CLI.

View details
91
Repo Health
83
Technical
65
Dependency
Built with
TypeScript 67%
Vue 25%
Updated 6 days ago

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers