diff options
| author | DragonBillow <DragonBillow@outlook.com> | 2023-07-25 12:21:30 +0800 |
|---|---|---|
| committer | DragonBillow <DragonBillow@outlook.com> | 2023-07-25 12:21:30 +0800 |
| commit | 40dd5a337cf8e565ae8c364e69e4e4e2d83ea4a7 (patch) | |
| tree | 5fdbb5ee69c83cd5d09c5a6e8e3b8041a37f578a /library/std/src/sync/lazy_lock.rs | |
| parent | beef07fe8fb98aca9f2b59c6a1b3e4f3ea4c0e15 (diff) | |
| download | rust-40dd5a337cf8e565ae8c364e69e4e4e2d83ea4a7.tar.gz rust-40dd5a337cf8e565ae8c364e69e4e4e2d83ea4a7.zip | |
docs(LazyLock): add example pass local LazyLock variable to struct
Signed-off-by: DragonBillow <DragonBillow@outlook.com>
Diffstat (limited to 'library/std/src/sync/lazy_lock.rs')
| -rw-r--r-- | library/std/src/sync/lazy_lock.rs | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/library/std/src/sync/lazy_lock.rs b/library/std/src/sync/lazy_lock.rs index a6bc468b092..37bdec5abcc 100644 --- a/library/std/src/sync/lazy_lock.rs +++ b/library/std/src/sync/lazy_lock.rs @@ -25,6 +25,8 @@ union Data<T, F> { /// /// # Examples /// +/// Initialize static variables with `LazyLock`. +/// /// ``` /// #![feature(lazy_cell)] /// @@ -54,6 +56,24 @@ union Data<T, F> { /// // Some("Hoyten") /// } /// ``` +/// Initialize fields with `LazyLock`. +/// ``` +/// #![feature(lazy_cell)] +/// +/// use std::sync::LazyLock; +/// +/// #[derive(Debug)] +/// struct UseCellLock { +/// number: LazyLock<u32>, +/// } +/// fn main() { +/// let lock: LazyLock<u32> = LazyLock::new(|| 0u32); +/// +/// let data = UseCellLock { number: lock }; +/// println!("{}", *data.number); +/// } +/// ``` + #[unstable(feature = "lazy_cell", issue = "109736")] pub struct LazyLock<T, F = fn() -> T> { once: Once, |
