about summary refs log tree commit diff
path: root/library/std/src/sync/lazy_lock.rs
diff options
context:
space:
mode:
authorJubilee Young <workingjubilee@gmail.com>2024-05-27 13:02:39 -0700
committerJubilee Young <workingjubilee@gmail.com>2024-06-02 22:53:41 -0700
commit2d0ebca9798f76ff5ff1ef171cdf73fb5c8d7127 (patch)
tree1dd9a289625d681a108e72a2dd675de15bb3f4fd /library/std/src/sync/lazy_lock.rs
parentfdb96f2123a8306e24f6ef0c77296bf0d2f9af99 (diff)
downloadrust-2d0ebca9798f76ff5ff1ef171cdf73fb5c8d7127.tar.gz
rust-2d0ebca9798f76ff5ff1ef171cdf73fb5c8d7127.zip
Move first OnceLock example to LazyLock
This example is spiritually an example of LazyLock, as it computes a
variable at runtime but accepts no inputs into that process.
It is also slightly simpler and thus easier to understand.
Change it to an even-more concise version and move it to LazyLock.

The example now editorializes slightly more. This may be unnecessary,
but it can be educational for the reader.
Diffstat (limited to 'library/std/src/sync/lazy_lock.rs')
-rw-r--r--library/std/src/sync/lazy_lock.rs36
1 files changed, 14 insertions, 22 deletions
diff --git a/library/std/src/sync/lazy_lock.rs b/library/std/src/sync/lazy_lock.rs
index d3bb3bfdff9..7a2eed93dd4 100644
--- a/library/std/src/sync/lazy_lock.rs
+++ b/library/std/src/sync/lazy_lock.rs
@@ -29,34 +29,26 @@ union Data<T, F> {
 /// # Examples
 ///
 /// Initialize static variables with `LazyLock`.
-///
 /// ```
-/// use std::collections::HashMap;
-///
 /// use std::sync::LazyLock;
 ///
-/// static HASHMAP: LazyLock<HashMap<i32, String>> = LazyLock::new(|| {
-///     println!("initializing");
-///     let mut m = HashMap::new();
-///     m.insert(13, "Spica".to_string());
-///     m.insert(74, "Hoyten".to_string());
-///     m
+/// // n.b. static items do not call [`Drop`] on program termination, so this won't be deallocated.
+/// // this is fine, as the OS can deallocate the terminated program faster than we can free memory
+/// // but tools like valgrind might report "memory leaks" as it isn't obvious this is intentional.
+/// static DEEP_THOUGHT: LazyLock<String> = LazyLock::new(|| {
+/// # mod another_crate {
+/// #     pub fn great_question() -> String { "42".to_string() }
+/// # }
+///     // M3 Ultra takes about 16 million years in --release config
+///     another_crate::great_question()
 /// });
 ///
-/// fn main() {
-///     println!("ready");
-///     std::thread::spawn(|| {
-///         println!("{:?}", HASHMAP.get(&13));
-///     }).join().unwrap();
-///     println!("{:?}", HASHMAP.get(&74));
-///
-///     // Prints:
-///     //   ready
-///     //   initializing
-///     //   Some("Spica")
-///     //   Some("Hoyten")
-/// }
+/// // The `String` is built, stored in the `LazyLock`, and returned as `&String`.
+/// let _ = &*DEEP_THOUGHT;
+/// // The `String` is retrieved from the `LazyLock` and returned as `&String`.
+/// let _ = &*DEEP_THOUGHT;
 /// ```
+///
 /// Initialize fields with `LazyLock`.
 /// ```
 /// use std::sync::LazyLock;