Debian Codegraph @

debian/missing-sources/lazy_static/README.md

raw symbols SCIP
1 lazy-static.rs
2 ==============
3
4 A macro for declaring lazily evaluated statics in Rust.
5
6 Using this macro, it is possible to have `static`s that require code to be
7 executed at runtime in order to be initialized.
8 This includes anything requiring heap allocations, like vectors or hash maps,
9 as well as anything that requires non-const function calls to be computed.
10
11 [![Rust](https://github.com/rust-lang-nursery/lazy-static.rs/actions/workflows/rust.yml/badge.svg)](https://github.com/rust-lang-nursery/lazy-static.rs/actions/workflows/rust.yml)
12 [![Latest version](https://img.shields.io/crates/v/lazy_static.svg)](https://crates.io/crates/lazy_static)
13 [![Documentation](https://docs.rs/lazy_static/badge.svg)](https://docs.rs/lazy_static)
14 [![License](https://img.shields.io/crates/l/lazy_static.svg)](https://github.com/rust-lang-nursery/lazy-static.rs#license)
15
16 ## Minimum supported `rustc`
17
18 `1.40.0+`
19
20 This version is explicitly tested in CI and may only be bumped in new minor versions. Any changes to the supported minimum version will be called out in the release notes.
21
22
23 # Getting Started
24
25 [lazy-static.rs is available on crates.io](https://crates.io/crates/lazy_static).
26 It is recommended to look there for the newest released version, as well as links to the newest builds of the docs.
27
28 At the point of the last update of this README, the latest published version could be used like this:
29
30 Add the following dependency to your Cargo manifest...
31
32 ```toml
33 [dependencies]
34 lazy_static = "1.5.0"
35 ```
36
37 ...and see the [docs](https://docs.rs/lazy_static) for how to use it.
38
39 # Example
40
41 ```rust
42 use lazy_static::lazy_static;
43 use std::collections::HashMap;
44
45 lazy_static! {
46 static ref HASHMAP: HashMap<u32, &'static str> = {
47 let mut m = HashMap::new();
48 m.insert(0, "foo");
49 m.insert(1, "bar");
50 m.insert(2, "baz");
51 m
52 };
53 }
54
55 fn main() {
56 // First access to `HASHMAP` initializes it
57 println!("The entry for `0` is \"{}\".", HASHMAP.get(&0).unwrap());
58
59 // Any further access to `HASHMAP` just returns the computed value
60 println!("The entry for `1` is \"{}\".", HASHMAP.get(&1).unwrap());
61 }
62 ```
63
64 # Standard library
65
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 be also be written as:
67
68 ```rust
69 use std::collections::HashMap;
70 use std::sync::OnceLock;
71
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 }
82
83 fn main() {
84 // First access to `HASHMAP` initializes it
85 println!("The entry for `0` is \"{}\".", hashmap().get(&0).unwrap());
86
87 // Any further access to `HASHMAP` just returns the computed value
88 println!("The entry for `1` is \"{}\".", hashmap().get(&1).unwrap());
89 }
90 ```
91
92 ## License
93
94 Licensed under either of
95
96 * Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or https://www.apache.org/licenses/LICENSE-2.0)
97 * MIT license ([LICENSE-MIT](LICENSE-MIT) or https://opensource.org/licenses/MIT)
98
99 at your option.
100
101 ### Contribution
102
103 Unless you explicitly state otherwise, any contribution intentionally submitted
104 for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any
105 additional terms or conditions.