1ef40d7f6Sopenharmony_ci<p align="center"><img src="design/logo.png" alt="once_cell"></p> 2ef40d7f6Sopenharmony_ci 3ef40d7f6Sopenharmony_ci 4ef40d7f6Sopenharmony_ci[](https://github.com/matklad/once_cell/actions) 5ef40d7f6Sopenharmony_ci[](https://crates.io/crates/once_cell) 6ef40d7f6Sopenharmony_ci[](https://docs.rs/once_cell/) 7ef40d7f6Sopenharmony_ci 8ef40d7f6Sopenharmony_ci# Overview 9ef40d7f6Sopenharmony_ci 10ef40d7f6Sopenharmony_ci`once_cell` provides two new cell-like types, `unsync::OnceCell` and `sync::OnceCell`. `OnceCell` 11ef40d7f6Sopenharmony_cimight store arbitrary non-`Copy` types, can be assigned to at most once and provide direct access 12ef40d7f6Sopenharmony_cito the stored contents. In a nutshell, API looks *roughly* like this: 13ef40d7f6Sopenharmony_ci 14ef40d7f6Sopenharmony_ci```rust 15ef40d7f6Sopenharmony_ciimpl OnceCell<T> { 16ef40d7f6Sopenharmony_ci fn new() -> OnceCell<T> { ... } 17ef40d7f6Sopenharmony_ci fn set(&self, value: T) -> Result<(), T> { ... } 18ef40d7f6Sopenharmony_ci fn get(&self) -> Option<&T> { ... } 19ef40d7f6Sopenharmony_ci} 20ef40d7f6Sopenharmony_ci``` 21ef40d7f6Sopenharmony_ci 22ef40d7f6Sopenharmony_ciNote that, like with `RefCell` and `Mutex`, the `set` method requires only a shared reference. 23ef40d7f6Sopenharmony_ciBecause of the single assignment restriction `get` can return an `&T` instead of `Ref<T>` 24ef40d7f6Sopenharmony_cior `MutexGuard<T>`. 25ef40d7f6Sopenharmony_ci 26ef40d7f6Sopenharmony_ci`once_cell` also has a `Lazy<T>` type, build on top of `OnceCell` which provides the same API as 27ef40d7f6Sopenharmony_cithe `lazy_static!` macro, but without using any macros: 28ef40d7f6Sopenharmony_ci 29ef40d7f6Sopenharmony_ci```rust 30ef40d7f6Sopenharmony_ciuse std::{sync::Mutex, collections::HashMap}; 31ef40d7f6Sopenharmony_ciuse once_cell::sync::Lazy; 32ef40d7f6Sopenharmony_ci 33ef40d7f6Sopenharmony_cistatic GLOBAL_DATA: Lazy<Mutex<HashMap<i32, String>>> = Lazy::new(|| { 34ef40d7f6Sopenharmony_ci let mut m = HashMap::new(); 35ef40d7f6Sopenharmony_ci m.insert(13, "Spica".to_string()); 36ef40d7f6Sopenharmony_ci m.insert(74, "Hoyten".to_string()); 37ef40d7f6Sopenharmony_ci Mutex::new(m) 38ef40d7f6Sopenharmony_ci}); 39ef40d7f6Sopenharmony_ci 40ef40d7f6Sopenharmony_cifn main() { 41ef40d7f6Sopenharmony_ci println!("{:?}", GLOBAL_DATA.lock().unwrap()); 42ef40d7f6Sopenharmony_ci} 43ef40d7f6Sopenharmony_ci``` 44ef40d7f6Sopenharmony_ci 45ef40d7f6Sopenharmony_ciMore patterns and use-cases are in the [docs](https://docs.rs/once_cell/)! 46ef40d7f6Sopenharmony_ci 47ef40d7f6Sopenharmony_ci# Related crates 48ef40d7f6Sopenharmony_ci 49ef40d7f6Sopenharmony_ci* [double-checked-cell](https://github.com/niklasf/double-checked-cell) 50ef40d7f6Sopenharmony_ci* [lazy-init](https://crates.io/crates/lazy-init) 51ef40d7f6Sopenharmony_ci* [lazycell](https://crates.io/crates/lazycell) 52ef40d7f6Sopenharmony_ci* [mitochondria](https://crates.io/crates/mitochondria) 53ef40d7f6Sopenharmony_ci* [lazy_static](https://crates.io/crates/lazy_static) 54ef40d7f6Sopenharmony_ci* [async_once_cell](https://crates.io/crates/async_once_cell) 55ef40d7f6Sopenharmony_ci* [generic_once_cell](https://crates.io/crates/generic_once_cell) (bring your own mutex) 56ef40d7f6Sopenharmony_ci 57ef40d7f6Sopenharmony_ciThe API of `once_cell` is being proposed for inclusion in 58ef40d7f6Sopenharmony_ci[`std`](https://github.com/rust-lang/rfcs/pull/2788). 59