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 26 apps in this directory
Meetily
AI Assistants · Productivity
Privacy-first AI meeting assistant that transcribes and summarizes your meetings entirely on your local machine — no cloud, no data leakage.
Meilisearch
Search
Lightning-fast hybrid search engine with AI-powered semantic and full-text retrieval for modern applications.
MicroBin
File Storage
A self-contained, encrypted paste bin and file-sharing app in Rust with animal-name URLs, burn-after-read, and one-command Docker deployment.
OpenBB
Analytics · Databases · Invoicing Finance
The AI Workspace for Finance: Connect Data, Run AI Agents, Build Analytics
ParadeDB
Analytics · Databases · Search
Born out of Y Combinator's S2023 batch, ParadeDB is a Postgres extension that delivers Elasticsearch-quality BM25 search and real-time analytics without a separate search cluster to manage.
Portpal
Developer Tools · Monitoring
A native desktop dashboard that shows every port your machine is listening on, kills the process blocking it in one click, and visualizes how your services are actually connected.
PostgresML
AI Development · Databases
Run ML training and LLM inference natively inside PostgreSQL with GPU acceleration — no data movement required.
QuestDB
Analytics · Databases
A high-performance, open-source time-series database built for financial market data, IoT telemetry, and real-time analytics, combining a zero-GC Java/C++ core with SIMD-accelerated SQL and a WAL-to-Parquet storage engine.
Readur
Bookmarks Archiving · File Storage · Knowledge Management
A self-hosted document management system that OCRs, indexes, and makes every PDF, scan, and Office file instantly searchable.