Skip to content

Commit

Permalink
Move first OnceLock example to LazyLock
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
workingjubilee committed Jun 3, 2024
1 parent 7e47256 commit 6d001c5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 58 deletions.
36 changes: 14 additions & 22 deletions std/src/sync/lazy_lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
36 changes: 0 additions & 36 deletions std/src/sync/once_lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
///
/// ```
Expand Down

0 comments on commit 6d001c5

Please sign in to comment.