once_cell

Rust library for single assignment cells and lazy statics without macros

Latest version: 1.20.2 registry icon
Maintenance score
84
Safety score
99
Popularity score
66
Check your open source dependency risks. Get immediate insight about security, stability and licensing risks.
Security
  Vulnerabilities
Version Suggest Low Medium High Critical
1.20.2 0 0 0 0 0
1.20.1 0 0 0 0 0
1.20.0 0 0 0 0 0
1.19.0 0 0 0 0 0
1.18.0 0 0 0 0 0
1.17.2 0 0 0 0 0
1.17.1 0 0 0 0 0
1.17.0 0 0 0 0 0
1.16.0 0 0 0 0 0
1.15.0 0 0 0 0 0
1.14.0 0 0 0 0 0
1.13.1 0 0 0 0 0
1.13.0 0 0 0 0 0
1.12.1 0 0 0 0 0
1.12.0 0 0 0 0 0
1.11.0 0 0 0 0 0
1.10.0 0 0 0 0 0
1.9.0 0 0 0 0 0
1.8.0 0 0 0 0 0
1.7.2 0 0 0 0 0
1.7.1 0 0 0 0 0
1.7.0 0 0 0 0 0
1.6.0 0 0 0 0 0
1.5.2 0 0 0 0 0
1.5.1 0 0 0 0 0
1.5.0 0 0 0 0 0
1.4.1 0 0 0 0 0
1.4.0 0 0 0 0 0
1.3.1 0 0 0 0 0
1.3.0 0 0 0 0 0
1.2.0 0 0 0 0 0
1.1.0 0 0 0 0 0
1.0.2 0 0 0 0 0
1.0.1 0 0 0 0 0
1.0.0 0 0 1 0 0
0.2.7 0 0 1 0 0
0.2.6 0 0 1 0 0
0.2.5 0 0 1 0 0
0.2.4 0 0 0 0 0
0.2.3 0 0 0 0 0
0.2.2 0 0 0 0 0
0.2.1 0 0 0 0 0
0.2.0 0 0 0 0 0
0.1.8 0 0 0 0 0
0.1.7 0 0 0 0 0
0.1.6 0 0 0 0 0
0.1.5 0 0 0 0 0
0.1.4 0 0 0 0 0
0.1.3 0 0 0 0 0
0.1.2 0 0 0 0 0
0.1.1 0 0 0 0 0
0.1.0 0 0 0 0 0
0.0.3 0 0 0 0 0
0.0.2 0 0 0 0 0
0.0.1 0 0 0 0 0

Stability
Latest release:

1.20.2 - This version is safe to use because it has no known security vulnerabilities at this time. Find out if your coding project uses this component and get notified of any reported security vulnerabilities with Meterian-X Open Source Security Platform

Licensing

Maintain your licence declarations and avoid unwanted licences to protect your IP the way you intended.

Apache-2.0   -   Apache License 2.0

Not a wildcard

Not proprietary

OSI Compliant


MIT   -   MIT License

Not a wildcard

Not proprietary

OSI Compliant



once_cell

Build Status Crates.io API reference

Overview

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());
    Mutex::new(m)
});

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

More patterns and use-cases are in the docs!

Related crates

Parts of once_cell API are included into std as of Rust 1.70.0.