]>
Commit | Line | Data |
---|---|---|
8bb4bdeb XL |
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 | ||
48663c56 | 11 | [![Travis-CI Status](https://travis-ci.com/rust-lang-nursery/lazy-static.rs.svg?branch=master)](https://travis-ci.com/rust-lang-nursery/lazy-static.rs) |
8faf50e0 XL |
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 | ||
0731742a XL |
16 | ## Minimum supported `rustc` |
17 | ||
dfeec247 | 18 | `1.27.2+` |
0731742a XL |
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 | ||
8bb4bdeb XL |
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] | |
dfeec247 | 34 | lazy_static = "1.4.0" |
8bb4bdeb XL |
35 | ``` |
36 | ||
abe05a73 | 37 | ...and see the [docs](https://docs.rs/lazy_static) for how to use it. |
8bb4bdeb XL |
38 | |
39 | # Example | |
40 | ||
41 | ```rust | |
42 | #[macro_use] | |
43 | extern crate lazy_static; | |
44 | ||
45 | use std::collections::HashMap; | |
46 | ||
47 | lazy_static! { | |
48 | static ref HASHMAP: HashMap<u32, &'static str> = { | |
49 | let mut m = HashMap::new(); | |
50 | m.insert(0, "foo"); | |
51 | m.insert(1, "bar"); | |
52 | m.insert(2, "baz"); | |
53 | m | |
54 | }; | |
55 | } | |
56 | ||
57 | fn main() { | |
58 | // First access to `HASHMAP` initializes it | |
59 | println!("The entry for `0` is \"{}\".", HASHMAP.get(&0).unwrap()); | |
60 | ||
61 | // Any further access to `HASHMAP` just returns the computed value | |
62 | println!("The entry for `1` is \"{}\".", HASHMAP.get(&1).unwrap()); | |
63 | } | |
64 | ``` | |
7cac9316 XL |
65 | |
66 | ## License | |
67 | ||
68 | Licensed under either of | |
69 | ||
70 | * Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) | |
71 | * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) | |
72 | ||
73 | at your option. | |
74 | ||
75 | ### Contribution | |
76 | ||
77 | Unless you explicitly state otherwise, any contribution intentionally submitted | |
78 | for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any | |
79 | additional terms or conditions. |