From 41fa23457d65b5c4688a42228734fd8479214a83 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Sat, 25 Nov 2023 12:08:12 -0500 Subject: [PATCH] Update README to indicate how to replace with `std::sync::OnceLock` --- README.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/README.md b/README.md index bd30c61..0bba4de 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,7 @@ as well as anything that requires non-const function calls to be computed. [![Documentation](https://docs.rs/lazy_static/badge.svg)](https://docs.rs/lazy_static) [![License](https://img.shields.io/crates/l/lazy_static.svg)](https://github.com/rust-lang-nursery/lazy-static.rs#license) + ## Minimum supported `rustc` `1.40.0+` @@ -61,6 +62,35 @@ 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 be also be written as: + +```rust +use std::collections::HashMap; +use std::sync::OnceLock; + +static HASHMAP: OnceLock> = OnceLock::new(); + +fn hashmap() -> &'static HashMap { + HASHMAP.get_or_init(|| { + 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()); + + // Any further access to `HASHMAP` just returns the computed value + println!("The entry for `1` is \"{}\".", hashmap().get(&1).unwrap()); +} +``` + ## License Licensed under either of