@@ -17,25 +17,36 @@ use crate::sync::Once;
17
17
/// ‘lazy static’ or ‘memoizing’):
18
18
///
19
19
/// ```
20
- /// use std::collections::HashMap;
21
20
/// use std::sync::OnceLock;
22
21
///
23
- /// fn hash_map() -> &'static HashMap<u32, char> {
24
- /// static HASHMAP: OnceLock<HashMap<u32, char>> = OnceLock::new();
25
- /// HASHMAP.get_or_init(|| {
26
- /// let mut m = HashMap::new();
27
- /// m.insert(0, 'a');
28
- /// m.insert(1, 'b');
29
- /// m.insert(2, 'c');
30
- /// m
31
- /// })
22
+ /// struct DeepThought {
23
+ /// answer: String,
32
24
/// }
33
25
///
34
- /// // The `HashMap` is built, stored in the `OnceLock`, and returned.
35
- /// let _ = hash_map();
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
+ /// }
36
45
///
37
- /// // The `HashMap` is retrieved from the `OnceLock` and returned.
38
- /// let _ = hash_map();
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;
39
50
/// ```
40
51
///
41
52
/// Writing to a `OnceLock` from a separate thread:
0 commit comments