once_cell
Single-assignment cells and lazy statics for Rust, without macros or unsafe code
Repository Health
Technical Analysis
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-Synclazy initializationsync::OnceCell<T>andsync::Lazy<T>for thread-safe one-time initialization backed by atomics- A
racemodule of lock-free cells (OnceBox, etc.) forno_stdtargets via thealloc/portable-atomic/critical-sectionfeatures - Optional
parking_lotbackend to shrinkOnceCell<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 ergonomicLazy<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 therace/critical-sectionfeature 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 21 apps in this directory
AppFlowy
Productivity · Project Management · Collaboration
The open-source AI workspace that puts your data, your rules — with local LLMs, CRDT collaboration, and full self-hosting built in.
CubeSandbox
Developer Tools · Security · AI Agents
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.
Fluree DB
Databases
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.
Fyrox
Game Development
A production-ready 2D/3D game engine written in Rust with a built-in scene editor, physics, and hot-reloading game scripts
Handy
Productivity
Free, offline, open-source speech-to-text that pastes directly into any app on Windows, macOS, and Linux.
hoop
Security · Monitoring
A wire-protocol gateway that enforces data masking, command blocking, approval workflows, and full session recording for engineers and AI agents accessing production infrastructure.
iii
Developer Tools · Devops
Compose, extend, and observe every backend service in real time using three primitives: Workers, Functions, and Triggers.
Joplin
Note Taking
The privacy-first, open-source note-taking app with end-to-end encrypted sync, AI assistance, and a powerful plugin ecosystem across every platform.
Lokus
Note Taking · Knowledge Management
Local-first note-taking with graph view, canvas & AI plugins—your Markdown files, zero telemetry, blazing-fast Rust performance.