]>
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 | ||
11 | [![Travis-CI Status](https://travis-ci.org/rust-lang-nursery/lazy-static.rs.svg?branch=master)](https://travis-ci.org/rust-lang-nursery/lazy-static.rs) | |
12 | ||
13 | # Getting Started | |
14 | ||
15 | [lazy-static.rs is available on crates.io](https://crates.io/crates/lazy_static). | |
16 | It is recommended to look there for the newest released version, as well as links to the newest builds of the docs. | |
17 | ||
18 | At the point of the last update of this README, the latest published version could be used like this: | |
19 | ||
20 | Add the following dependency to your Cargo manifest... | |
21 | ||
22 | ```toml | |
23 | [dependencies] | |
24 | lazy_static = "0.2" | |
25 | ``` | |
26 | ||
27 | ...and see the [docs](http://rust-lang-nursery.github.io/lazy-static.rs/lazy_static/index.html) for how to use it. | |
28 | ||
29 | # Example | |
30 | ||
31 | ```rust | |
32 | #[macro_use] | |
33 | extern crate lazy_static; | |
34 | ||
35 | use std::collections::HashMap; | |
36 | ||
37 | lazy_static! { | |
38 | static ref HASHMAP: HashMap<u32, &'static str> = { | |
39 | let mut m = HashMap::new(); | |
40 | m.insert(0, "foo"); | |
41 | m.insert(1, "bar"); | |
42 | m.insert(2, "baz"); | |
43 | m | |
44 | }; | |
45 | } | |
46 | ||
47 | fn main() { | |
48 | // First access to `HASHMAP` initializes it | |
49 | println!("The entry for `0` is \"{}\".", HASHMAP.get(&0).unwrap()); | |
50 | ||
51 | // Any further access to `HASHMAP` just returns the computed value | |
52 | println!("The entry for `1` is \"{}\".", HASHMAP.get(&1).unwrap()); | |
53 | } | |
54 | ``` | |
7cac9316 XL |
55 | |
56 | ## License | |
57 | ||
58 | Licensed under either of | |
59 | ||
60 | * Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) | |
61 | * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) | |
62 | ||
63 | at your option. | |
64 | ||
65 | ### Contribution | |
66 | ||
67 | Unless you explicitly state otherwise, any contribution intentionally submitted | |
68 | for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any | |
69 | additional terms or conditions. |