about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2023-07-25 19:21:37 +0200
committerGitHub <noreply@github.com>2023-07-25 19:21:37 +0200
commit91d1d7aa4401d03c431aed47f8a07240db2382e7 (patch)
treef7c42b06e7fe899baead823a994e8be50b99199b
parent8ecaf2ae57a8bb02731b6052d6807542a20aca27 (diff)
parent40dd5a337cf8e565ae8c364e69e4e4e2d83ea4a7 (diff)
downloadrust-91d1d7aa4401d03c431aed47f8a07240db2382e7.tar.gz
rust-91d1d7aa4401d03c431aed47f8a07240db2382e7.zip
Rollup merge of #114043 - cathaysia:doc_lazy_lock, r=thomcc
docs(LazyLock): add example pass local LazyLock variable to struct
-rw-r--r--library/std/src/sync/lazy_lock.rs20
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,