Skip to content

Commit f9ee0cc

Browse files
authored
Merge pull request #225 from josephcsible/patch-1
Use LazyLock instead of OnceLock in the std example
2 parents cc81425 + e5730c6 commit f9ee0cc

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed

README.md

+11-14
Original file line numberDiff line numberDiff line change
@@ -63,29 +63,26 @@ fn main() {
6363

6464
# Standard library
6565

66-
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:
66+
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:
6767

6868
```rust
6969
use std::collections::HashMap;
70-
use std::sync::OnceLock;
70+
use std::sync::LazyLock;
7171

72-
fn hashmap() -> &'static HashMap<u32, &'static str> {
73-
static HASHMAP: OnceLock<HashMap<u32, &str>> = OnceLock::new();
74-
HASHMAP.get_or_init(|| {
75-
let mut m = HashMap::new();
76-
m.insert(0, "foo");
77-
m.insert(1, "bar");
78-
m.insert(2, "baz");
79-
m
80-
})
81-
}
72+
static HASHMAP: LazyLock<HashMap<u32, &str>> = LazyLock::new(|| {
73+
let mut m = HashMap::new();
74+
m.insert(0, "foo");
75+
m.insert(1, "bar");
76+
m.insert(2, "baz");
77+
m
78+
});
8279

8380
fn main() {
8481
// First access to `HASHMAP` initializes it
85-
println!("The entry for `0` is \"{}\".", hashmap().get(&0).unwrap());
82+
println!("The entry for `0` is \"{}\".", HASHMAP.get(&0).unwrap());
8683

8784
// Any further access to `HASHMAP` just returns the computed value
88-
println!("The entry for `1` is \"{}\".", hashmap().get(&1).unwrap());
85+
println!("The entry for `1` is \"{}\".", HASHMAP.get(&1).unwrap());
8986
}
9087
```
9188

0 commit comments

Comments
 (0)