1d0a2ff35Sopenharmony_cilazy-static.rs 2d0a2ff35Sopenharmony_ci============== 3d0a2ff35Sopenharmony_ci 4d0a2ff35Sopenharmony_ciA macro for declaring lazily evaluated statics in Rust. 5d0a2ff35Sopenharmony_ci 6d0a2ff35Sopenharmony_ciUsing this macro, it is possible to have `static`s that require code to be 7d0a2ff35Sopenharmony_ciexecuted at runtime in order to be initialized. 8d0a2ff35Sopenharmony_ciThis includes anything requiring heap allocations, like vectors or hash maps, 9d0a2ff35Sopenharmony_cias well as anything that requires non-const function calls to be computed. 10d0a2ff35Sopenharmony_ci 11d0a2ff35Sopenharmony_ci[](https://travis-ci.com/rust-lang-nursery/lazy-static.rs) 12d0a2ff35Sopenharmony_ci[](https://crates.io/crates/lazy_static) 13d0a2ff35Sopenharmony_ci[](https://docs.rs/lazy_static) 14d0a2ff35Sopenharmony_ci[](https://github.com/rust-lang-nursery/lazy-static.rs#license) 15d0a2ff35Sopenharmony_ci 16d0a2ff35Sopenharmony_ci## Minimum supported `rustc` 17d0a2ff35Sopenharmony_ci 18d0a2ff35Sopenharmony_ci`1.27.2+` 19d0a2ff35Sopenharmony_ci 20d0a2ff35Sopenharmony_ciThis 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. 21d0a2ff35Sopenharmony_ci 22d0a2ff35Sopenharmony_ci 23d0a2ff35Sopenharmony_ci# Getting Started 24d0a2ff35Sopenharmony_ci 25d0a2ff35Sopenharmony_ci[lazy-static.rs is available on crates.io](https://crates.io/crates/lazy_static). 26d0a2ff35Sopenharmony_ciIt is recommended to look there for the newest released version, as well as links to the newest builds of the docs. 27d0a2ff35Sopenharmony_ci 28d0a2ff35Sopenharmony_ciAt the point of the last update of this README, the latest published version could be used like this: 29d0a2ff35Sopenharmony_ci 30d0a2ff35Sopenharmony_ciAdd the following dependency to your Cargo manifest... 31d0a2ff35Sopenharmony_ci 32d0a2ff35Sopenharmony_ci```toml 33d0a2ff35Sopenharmony_ci[dependencies] 34d0a2ff35Sopenharmony_cilazy_static = "1.4.0" 35d0a2ff35Sopenharmony_ci``` 36d0a2ff35Sopenharmony_ci 37d0a2ff35Sopenharmony_ci...and see the [docs](https://docs.rs/lazy_static) for how to use it. 38d0a2ff35Sopenharmony_ci 39d0a2ff35Sopenharmony_ci# Example 40d0a2ff35Sopenharmony_ci 41d0a2ff35Sopenharmony_ci```rust 42d0a2ff35Sopenharmony_ci#[macro_use] 43d0a2ff35Sopenharmony_ciextern crate lazy_static; 44d0a2ff35Sopenharmony_ci 45d0a2ff35Sopenharmony_ciuse std::collections::HashMap; 46d0a2ff35Sopenharmony_ci 47d0a2ff35Sopenharmony_cilazy_static! { 48d0a2ff35Sopenharmony_ci static ref HASHMAP: HashMap<u32, &'static str> = { 49d0a2ff35Sopenharmony_ci let mut m = HashMap::new(); 50d0a2ff35Sopenharmony_ci m.insert(0, "foo"); 51d0a2ff35Sopenharmony_ci m.insert(1, "bar"); 52d0a2ff35Sopenharmony_ci m.insert(2, "baz"); 53d0a2ff35Sopenharmony_ci m 54d0a2ff35Sopenharmony_ci }; 55d0a2ff35Sopenharmony_ci} 56d0a2ff35Sopenharmony_ci 57d0a2ff35Sopenharmony_cifn main() { 58d0a2ff35Sopenharmony_ci // First access to `HASHMAP` initializes it 59d0a2ff35Sopenharmony_ci println!("The entry for `0` is \"{}\".", HASHMAP.get(&0).unwrap()); 60d0a2ff35Sopenharmony_ci 61d0a2ff35Sopenharmony_ci // Any further access to `HASHMAP` just returns the computed value 62d0a2ff35Sopenharmony_ci println!("The entry for `1` is \"{}\".", HASHMAP.get(&1).unwrap()); 63d0a2ff35Sopenharmony_ci} 64d0a2ff35Sopenharmony_ci``` 65d0a2ff35Sopenharmony_ci 66d0a2ff35Sopenharmony_ci## License 67d0a2ff35Sopenharmony_ci 68d0a2ff35Sopenharmony_ciLicensed under either of 69d0a2ff35Sopenharmony_ci 70d0a2ff35Sopenharmony_ci * Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) 71d0a2ff35Sopenharmony_ci * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) 72d0a2ff35Sopenharmony_ci 73d0a2ff35Sopenharmony_ciat your option. 74d0a2ff35Sopenharmony_ci 75d0a2ff35Sopenharmony_ci### Contribution 76d0a2ff35Sopenharmony_ci 77d0a2ff35Sopenharmony_ciUnless you explicitly state otherwise, any contribution intentionally submitted 78d0a2ff35Sopenharmony_cifor inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any 79d0a2ff35Sopenharmony_ciadditional terms or conditions. 80