Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
//! ## Safe Initialization of Global Data
//!
//! ```rust
//! # #[cfg(any(feature = "std", feature = "critical-section"))] {
//! use std::{env, io};
//!
//! use once_cell::sync::OnceCell;
Expand All @@ -60,13 +61,15 @@
//! INSTANCE.set(logger).unwrap();
//! // use `Logger::global()` from now on
//! }
//! # }
//! ```
//!
//! ## Lazy Initialized Global Data
//!
//! This is essentially the `lazy_static!` macro, but without a macro.
//!
//! ```rust
//! # #[cfg(any(feature = "std", feature = "critical-section"))] {
//! use std::{sync::Mutex, collections::HashMap};
//!
//! use once_cell::sync::OnceCell;
Expand All @@ -80,12 +83,14 @@
//! Mutex::new(m)
//! })
//! }
//! # }
//! ```
//!
//! There are also the [`sync::Lazy`] and [`unsync::Lazy`] convenience types to
//! streamline this pattern:
//!
//! ```rust
//! # #[cfg(any(feature = "std", feature = "critical-section"))] {
//! use std::{sync::Mutex, collections::HashMap};
//! use once_cell::sync::Lazy;
//!
Expand All @@ -99,6 +104,7 @@
//! fn main() {
//! println!("{:?}", GLOBAL_DATA.lock().unwrap());
//! }
//! # }
//! ```
//!
//! Note that the variable that holds `Lazy` is declared as `static`, *not*
Expand Down Expand Up @@ -171,6 +177,7 @@
//! runtime:
//!
//! ```
//! # #[cfg(any(feature = "std", feature = "critical-section"))] {
//! use std::path::Path;
//!
//! use once_cell::sync::OnceCell;
Expand Down Expand Up @@ -203,6 +210,7 @@
//! // ...
//! # drop(rgb);
//! }
//! # }
//! ```
//!
//! ## `lateinit`
Expand All @@ -212,6 +220,7 @@
//!
//!
//! ```
//! # #[cfg(any(feature = "std", feature = "critical-section"))] {
//! use once_cell::sync::OnceCell;
//!
//! pub struct LateInit<T> { cell: OnceCell<T> }
Expand Down Expand Up @@ -252,6 +261,7 @@
//!
//! let _a = &a.b.a.b.a;
//! }
//! # }
//! ```
//!
//! # Comparison with std
Expand Down
Loading