Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 6d001c5

Browse files
committedJun 3, 2024
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.
1 parent 7e47256 commit 6d001c5

File tree

2 files changed

+14
-58
lines changed

2 files changed

+14
-58
lines changed
 

‎std/src/sync/lazy_lock.rs

+14-22
Original file line numberDiff line numberDiff line change
@@ -29,34 +29,26 @@ union Data<T, F> {
2929
/// # Examples
3030
///
3131
/// Initialize static variables with `LazyLock`.
32-
///
3332
/// ```
34-
/// use std::collections::HashMap;
35-
///
3633
/// use std::sync::LazyLock;
3734
///
38-
/// static HASHMAP: LazyLock<HashMap<i32, String>> = LazyLock::new(|| {
39-
/// println!("initializing");
40-
/// let mut m = HashMap::new();
41-
/// m.insert(13, "Spica".to_string());
42-
/// m.insert(74, "Hoyten".to_string());
43-
/// m
35+
/// // n.b. static items do not call [`Drop`] on program termination, so this won't be deallocated.
36+
/// // this is fine, as the OS can deallocate the terminated program faster than we can free memory
37+
/// // but tools like valgrind might report "memory leaks" as it isn't obvious this is intentional.
38+
/// static DEEP_THOUGHT: LazyLock<String> = LazyLock::new(|| {
39+
/// # mod another_crate {
40+
/// # pub fn great_question() -> String { "42".to_string() }
41+
/// # }
42+
/// // M3 Ultra takes about 16 million years in --release config
43+
/// another_crate::great_question()
4444
/// });
4545
///
46-
/// fn main() {
47-
/// println!("ready");
48-
/// std::thread::spawn(|| {
49-
/// println!("{:?}", HASHMAP.get(&13));
50-
/// }).join().unwrap();
51-
/// println!("{:?}", HASHMAP.get(&74));
52-
///
53-
/// // Prints:
54-
/// // ready
55-
/// // initializing
56-
/// // Some("Spica")
57-
/// // Some("Hoyten")
58-
/// }
46+
/// // The `String` is built, stored in the `LazyLock`, and returned as `&String`.
47+
/// let _ = &*DEEP_THOUGHT;
48+
/// // The `String` is retrieved from the `LazyLock` and returned as `&String`.
49+
/// let _ = &*DEEP_THOUGHT;
5950
/// ```
51+
///
6052
/// Initialize fields with `LazyLock`.
6153
/// ```
6254
/// use std::sync::LazyLock;

‎std/src/sync/once_lock.rs

-36
Original file line numberDiff line numberDiff line change
@@ -13,42 +13,6 @@ use crate::sync::Once;
1313
///
1414
/// # Examples
1515
///
16-
/// Using `OnceLock` to store a function’s previously computed value (a.k.a.
17-
/// ‘lazy static’ or ‘memoizing’):
18-
///
19-
/// ```
20-
/// use std::sync::OnceLock;
21-
///
22-
/// struct DeepThought {
23-
/// answer: String,
24-
/// }
25-
///
26-
/// impl DeepThought {
27-
/// # fn great_question() -> String {
28-
/// # "42".to_string()
29-
/// # }
30-
/// #
31-
/// fn new() -> Self {
32-
/// Self {
33-
/// // M3 Ultra takes about 16 million years in --release config
34-
/// answer: Self::great_question(),
35-
/// }
36-
/// }
37-
/// }
38-
///
39-
/// fn computation() -> &'static DeepThought {
40-
/// // n.b. static items do not call [`Drop`] on program termination, so if
41-
/// // [`DeepThought`] impls Drop, that will not be used for this instance.
42-
/// static COMPUTATION: OnceLock<DeepThought> = OnceLock::new();
43-
/// COMPUTATION.get_or_init(|| DeepThought::new())
44-
/// }
45-
///
46-
/// // The `DeepThought` is built, stored in the `OnceLock`, and returned.
47-
/// let _ = computation().answer;
48-
/// // The `DeepThought` is retrieved from the `OnceLock` and returned.
49-
/// let _ = computation().answer;
50-
/// ```
51-
///
5216
/// Writing to a `OnceLock` from a separate thread:
5317
///
5418
/// ```

0 commit comments

Comments
 (0)
Failed to load comments.