Move first OnceLock example to LazyLock · model-checking/verify-rust-std@6d001c5 (original) (raw)
`@@ -29,34 +29,26 @@ union Data<T, F> {
`
29
29
`/// # Examples
`
30
30
`///
`
31
31
`` /// Initialize static variables with LazyLock
.
``
32
``
`-
///
`
33
32
```` /// ```
`34`
``
`-
/// use std::collections::HashMap;
`
`35`
``
`-
///
`
`36`
`33`
`/// use std::sync::LazyLock;
`
`37`
`34`
`///
`
`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()
`
`44`
`44`
`/// });
`
`45`
`45`
`///
`
`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;
`
`59`
`50`
```` /// ```
``
51
`+
///
`
60
52
`` /// Initialize fields with LazyLock
.
``
61
53
```` /// ```
````
62
54
`/// use std::sync::LazyLock;
`