once_cell
Single assignment cells and lazy values.
This package has a good security score with no known vulnerabilities.
Community Reviews
Zero-overhead lazy initialization that just works in production
The sync and unsync modules make thread-safety explicit, which prevents accidental performance penalties. I particularly appreciate that get_or_init and get_or_try_init return references directly without Box overhead. Memory usage is minimal - just the stored value plus a single atomic flag for sync types. The blocking behavior during concurrent initialization is well-documented and predictable under load.
Error handling with get_or_try_init is clean, though you need to understand that failed initializations don't poison the cell - subsequent calls will retry. This is actually the right default for most production scenarios where transient errors shouldn't permanently break your application state.
Best for: Lazy initialization of expensive resources like connection pools, compiled regexes, or config objects that need guaranteed single initialization.
Avoid if: You need std::sync::OnceLock from Rust 1.70+ which is now stabilized in the standard library with identical semantics.
Rock-solid lazy initialization with minimal security surface area
The library handles initialization errors cleanly without exposing internal state. Poisoning behavior is explicit and well-documented, preventing race conditions in multi-threaded contexts. Input validation is your responsibility (as it should be), but once_cell never silently swallows panics or creates undefined behavior. The sync and unsync variants make thread-safety guarantees clear at compile time.
CVE response has been solid—the maintainer addresses issues promptly when they arise. The codebase is small enough to audit yourself in an afternoon. Now that std::sync::LazyLock and std::cell::LazyCell are stabilizing in newer Rust versions, once_cell serves as either a polyfill or direct replacement depending on your MSRV requirements.
Best for: Projects requiring lazy initialization or global state with strict security requirements and minimal dependency footprint.
Avoid if: You're on Rust 1.80+ and can use std::sync::LazyLock directly without compatibility concerns.
Ergonomic lazy initialization that just works
Documentation is excellent with clear examples for common patterns like lazy regex compilation, config initialization, and caching. Error messages are straightforward when you hit issues like attempting multiple initializations. The crate integrates seamlessly with IDE tooling - autocomplete suggestions are spot-on, and type inference works without fighting the compiler.
Migration between versions has been painless in my experience. The API is stable and well-thought-out. While parts are being standardized into std::sync::OnceLock in newer Rust versions, once_cell remains the more feature-complete option with better ergonomics for complex initialization patterns.
Best for: Projects needing lazy static initialization, global caches, or expensive one-time resource setup with clean ergonomics.
Avoid if: You're on Rust 1.70+ and only need basic once-initialization without fallible init or lazy statics (stdlib OnceLock may suffice).
Sign in to write a review
Sign In