once_cell

Single-assignment cells and lazy statics for Rust, without macros or unsafe code

Library
Cargo
v1.21.4
2,089 stars
Apache License 2.0

Repository Health

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

Technical Analysis

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

once_cell provides OnceCell<T> and Lazy<T> types that can be assigned at most once, giving Rust code a safe way to perform one-time initialization of a value and then read it directly afterward — no unsafe, no macro-based indirection. It ships both a single-threaded unsync::OnceCell for non-shared state and a thread-safe sync::OnceCell/Lazy pair built on atomics (optionally backed by parking_lot for a smaller cell size), plus a race module of lock-free cells for no_std environments.

For years it was the de facto replacement for the lazy_static! macro and for hand-rolled unsafe global statics in the Rust ecosystem, and much of its sync API was eventually adopted into the standard library as std::sync::OnceLock/std::cell::OnceCell (stabilized in Rust 1.70). The crate remains widely depended on both for its Lazy type (which the standard library still lacks) and by codebases that predate or don’t want to bump their MSRV past the stabilization version.

What You Get

  • unsync::OnceCell<T> for single-threaded, non-Sync lazy initialization
  • sync::OnceCell<T> and sync::Lazy<T> for thread-safe one-time initialization backed by atomics
  • A race module of lock-free cells (OnceBox, etc.) for no_std targets via the alloc/portable-atomic/critical-section features
  • Optional parking_lot backend to shrink OnceCell<T>’s in-memory size
  • A stable, macro-free API surface that predates and substantially overlaps with the now-stabilized std::sync::OnceLock

Common Use Cases

  • Replacing lazy_static! macro usage with a macro-free, more ergonomic Lazy<T> static
  • Caching an expensive-to-compute value (parsed config, compiled regex, computed table) behind a single initialization point
  • Providing global, thread-safe application state that’s initialized once at first access rather than at program start
  • Implementing no_std/embedded one-time initialization via the race/critical-section feature set

Under The Hood

Architecture - The crate is split into unsync (in lib.rs) for single-threaded use and sync, backed by one of three interchangeable low-level implementations selected via Cargo features: imp_std.rs (atomics + std::sync primitives, ~415 lines), imp_pl.rs (a parking_lot_core-based implementation that trades a dependency for a smaller cell size), or imp_cs.rs (a critical-section-based implementation for no_std/embedded targets); a separate race.rs module implements lock-free “racy” cells (e.g. OnceBox) for contexts where blocking synchronization isn’t available. Tech Stack - Pure Rust (edition 2021, MSRV 1.65) with all synchronization dependencies gated behind opt-in features (parking_lot_core, portable-atomic, critical-section) so the default build depends on nothing beyond std; a xtask workspace member handles crate housekeeping. Code Quality - tests/ covers reentrant-init deadlock scenarios, cross-thread synchronization races, and no_std builds, and the examples/ directory includes purpose-built benchmarks (bench.rs, bench_acquire.rs) and a deadlock-reproduction example (reentrant_init_deadlocks.rs) that documents a known hazard of recursive initialization; the crate has a long, stable history with careful semver discipline. API Design - The API is deliberately minimal — get, set, get_or_init on OnceCell, and transparent Deref on Lazy — closely mirroring what later became std::sync::OnceLock, so most users can adopt it with almost no learning curve, and migrating to or from the standard-library equivalent is largely mechanical.

Used by 26 apps in this directory

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
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
Rust
99%
Other

Fluree DB

Databases

469

A temporal, verifiable graph database with git-like branching, integrated vector/text/geo search, and RDF/SPARQL/JSON-LD/openCypher support — benchmarked at 10.4x faster than the next database on the full Wikidata dump.

View details
87
Repo Health
74
Technical
65
Dependency
Built with
Rust 99%
Updated 6 days ago
Rust
95%
MIT

Fyrox

Game Development

9,568

A production-ready 2D/3D game engine written in Rust with a built-in scene editor, physics, and hot-reloading game scripts

View details
85
Repo Health
79
Technical
68
Dependency
Built with
Rust 95%
Updated 6 days ago
Rust
59%
MIT

Handy

Productivity · Voice AI

32,320

Free, offline, open-source speech-to-text that pastes directly into any app on Windows, macOS, and Linux.

View details
87
Repo Health
78
Technical
72
Dependency
Built with
Rust 59%
TypeScript 30%
Updated 5 days ago
Go
64%
MIT

hoop

Monitoring · Security

823

A wire-protocol gateway that enforces data masking, command blocking, approval workflows, and full session recording for engineers and AI agents accessing production infrastructure.

View details
85
Repo Health
80
Technical
64
Dependency
Built with
Go 64%
Clojure 17%
JavaScript 11%
Updated 1 weeks ago
Rust
73%
Other

iii

Developer Tools · Devops

18,814

Compose, extend, and observe every backend service in real time using three primitives: Workers, Functions, and Triggers.

View details
87
Repo Health
85
Technical
65
Dependency
Built with
Rust 73%
TypeScript 16%
Updated 6 days ago
TypeScript
77%
Other

Joplin

Note Taking

56,513

The privacy-first, open-source note-taking app with end-to-end encrypted sync, AI assistance, and a powerful plugin ecosystem across every platform.

View details
93
Repo Health
87
Technical
60
Dependency
Built with
TypeScript 77%
JavaScript 14%
Updated 6 days ago
JavaScript
55%
Other

Lokus

Knowledge Management · Note Taking

802

Local-first note-taking with graph view, canvas & AI plugins—your Markdown files, zero telemetry, blazing-fast Rust performance.

View details
78
Repo Health
75
Technical
65
Dependency
Built with
JavaScript 55%
HTML 21%
Rust 12%
Updated 1 months 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