From e5730c65820c06eba777e9aa1dbaf41808ec5e9b Mon Sep 17 00:00:00 2001 From: "Joseph C. Sible" Date: Sat, 21 Dec 2024 12:58:58 -0500 Subject: [PATCH] Use LazyLock instead of OnceLock in the std example --- README.md | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 55f0f7b..be7877a 100644 --- a/README.md +++ b/README.md @@ -63,29 +63,26 @@ fn main() { # Standard library -It is now possible to easily replicate this crate's functionality in Rust's standard library with [`std::sync::OnceLock`](https://doc.rust-lang.org/std/sync/struct.OnceLock.html). The example above could also be written as: +It is now possible to easily replicate this crate's functionality in Rust's standard library with [`std::sync::LazyLock`](https://doc.rust-lang.org/std/sync/struct.LazyLock.html). The example above could also be written as: ```rust use std::collections::HashMap; -use std::sync::OnceLock; +use std::sync::LazyLock; -fn hashmap() -> &'static HashMap { - static HASHMAP: OnceLock> = OnceLock::new(); - HASHMAP.get_or_init(|| { - let mut m = HashMap::new(); - m.insert(0, "foo"); - m.insert(1, "bar"); - m.insert(2, "baz"); - m - }) -} +static HASHMAP: LazyLock> = LazyLock::new(|| { + let mut m = HashMap::new(); + m.insert(0, "foo"); + m.insert(1, "bar"); + m.insert(2, "baz"); + m +}); fn main() { // First access to `HASHMAP` initializes it - println!("The entry for `0` is \"{}\".", hashmap().get(&0).unwrap()); + println!("The entry for `0` is \"{}\".", HASHMAP.get(&0).unwrap()); // Any further access to `HASHMAP` just returns the computed value - println!("The entry for `1` is \"{}\".", hashmap().get(&1).unwrap()); + println!("The entry for `1` is \"{}\".", HASHMAP.get(&1).unwrap()); } ```