mirror of https://github.com/rust-lang/rust.git
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.
This commit is contained in:
parent
fdb96f2123
commit
2d0ebca979
|
@ -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;
|
||||
|
|
|
@ -13,42 +13,6 @@ use crate::sync::Once;
|
|||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// Using `OnceLock` to store a function’s previously computed value (a.k.a.
|
||||
/// ‘lazy static’ or ‘memoizing’):
|
||||
///
|
||||
/// ```
|
||||
/// use std::sync::OnceLock;
|
||||
///
|
||||
/// struct DeepThought {
|
||||
/// answer: String,
|
||||
/// }
|
||||
///
|
||||
/// impl DeepThought {
|
||||
/// # fn great_question() -> String {
|
||||
/// # "42".to_string()
|
||||
/// # }
|
||||
/// #
|
||||
/// fn new() -> Self {
|
||||
/// Self {
|
||||
/// // M3 Ultra takes about 16 million years in --release config
|
||||
/// answer: Self::great_question(),
|
||||
/// }
|
||||
/// }
|
||||
/// }
|
||||
///
|
||||
/// fn computation() -> &'static DeepThought {
|
||||
/// // n.b. static items do not call [`Drop`] on program termination, so if
|
||||
/// // [`DeepThought`] impls Drop, that will not be used for this instance.
|
||||
/// static COMPUTATION: OnceLock<DeepThought> = OnceLock::new();
|
||||
/// COMPUTATION.get_or_init(|| DeepThought::new())
|
||||
/// }
|
||||
///
|
||||
/// // The `DeepThought` is built, stored in the `OnceLock`, and returned.
|
||||
/// let _ = computation().answer;
|
||||
/// // The `DeepThought` is retrieved from the `OnceLock` and returned.
|
||||
/// let _ = computation().answer;
|
||||
/// ```
|
||||
///
|
||||
/// Writing to a `OnceLock` from a separate thread:
|
||||
///
|
||||
/// ```
|
||||
|
|
Loading…
Reference in New Issue