Skip to content

Commit 213ae4a

Browse files
Explain LazyCell in core::cell overview
1 parent f6e4703 commit 213ae4a

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

library/core/src/cell.rs

+16
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,21 @@
8282
//!
8383
//! The corresponding [`Sync`] version of `OnceCell<T>` is [`OnceLock<T>`].
8484
//!
85+
//! ## `LazyCell<T, F>`
86+
//!
87+
//! A common pattern with OnceCell is, for a given OnceCell, to use a specific function on every
88+
//! call to [`OnceCell::get_or_init`] with that cell. This is what is offered by [`LazyCell`],
89+
//! which pairs cells of `T` with functions of `F`, and calls `F` before it yields `&T`.
90+
//! This happens implicitly by simply attempting to dereference the LazyCell to get its contents,
91+
//! so its use is much more transparent with a place which has been initialized by a constant.
92+
//!
93+
//! More complicated patterns that don't fit this description can be built on `OnceCell<T>` instead.
94+
//!
95+
//! `LazyCell` provides the following methods:
96+
//!
97+
//! - [`LazyCell::force`]: initialize the LazyCell and return a reference.
98+
//!
99+
//! The corresponding [`Sync`] version of `LazyCell<T, F>` is [`LazyLock<T, F>`].
85100
//!
86101
//! # When to choose interior mutability
87102
//!
@@ -230,6 +245,7 @@
230245
//! [`RwLock<T>`]: ../../std/sync/struct.RwLock.html
231246
//! [`Mutex<T>`]: ../../std/sync/struct.Mutex.html
232247
//! [`OnceLock<T>`]: ../../std/sync/struct.OnceLock.html
248+
//! [`LazyLock<T, F>`]: ../../std/sync/struct.LazyLock.html
233249
//! [`Sync`]: ../../std/marker/trait.Sync.html
234250
//! [`atomic`]: crate::sync::atomic
235251

0 commit comments

Comments
 (0)