Skip to content
Rust library for single assignment cells and lazy statics without macros
Rust Shell
Branch: master
Clone or download
bors and matklad Merge #88
88: Don't require Debug bound closure for Lazy r=matklad a=matklad

bors r+

Co-authored-by: Aleksey Kladov <>
Latest commit ea4634c Jan 17, 2020
Type Name Latest commit message Commit time
Failed to load latest commit information.
design Add files via upload Aug 8, 2018
examples Paper over unfortunate --all-targets interaction Jan 1, 2020
src Don't require Debug bound closure for Lazy Jan 17, 2020
tests Implement DerefMut for Lazy Jan 14, 2020
.gitignore Initial Aug 2, 2018
.travis.yml actually test parking_lot on CI Oct 21, 2019
Cargo.lock.min v0.2.0 May 7, 2019
Cargo.toml Don't require Debug bound closure for Lazy Jan 17, 2020
LICENSE-MIT Initial Aug 2, 2018 Release 1.3.0 Jan 14, 2020
logo.svg Miri: ignore memory leaks so that we can test more things Dec 9, 2019


Build Status API reference


once_cell provides two new cell-like types, unsync::OnceCell and sync::OnceCell. OnceCell might store arbitrary non-Copy types, can be assigned to at most once and provide direct access to the stored contents. In a nutshell, API looks roughly like this:

impl OnceCell<T> {
    fn new() -> OnceCell<T> { ... }
    fn set(&self, value: T) -> Result<(), T> { ... }
    fn get(&self) -> Option<&T> { ... }

Note that, like with RefCell and Mutex, the set method requires only a shared reference. Because of the single assignment restriction get can return an &T instead of Ref<T> or MutexGuard<T>.

once_cell also has a Lazy<T> type, build on top of OnceCell which provides the same API as the lazy_static! macro, but without using any macros:

use std::{sync::Mutex, collections::HashMap};
use once_cell::sync::Lazy;

static GLOBAL_DATA: Lazy<Mutex<HashMap<i32, String>>> = Lazy::new(|| {
    let mut m = HashMap::new();
    m.insert(13, "Spica".to_string());
    m.insert(74, "Hoyten".to_string());

fn main() {
    println!("{:?}", GLOBAL_DATA.lock().unwrap());

More patterns and use-cases are in the docs!

Related crates

The API of once_cell is being proposed for inclusion in std.

You can’t perform that action at this time.