- Notifications
You must be signed in to change notification settings - Fork127
A small macro for defining lazy evaluated static variables in Rust.
License
Apache-2.0, MIT licenses found
Licenses found
rust-lang-nursery/lazy-static.rs
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
A macro for declaring lazily evaluated statics in Rust.
Using this macro, it is possible to havestatic
s that require code to beexecuted at runtime in order to be initialized.This includes anything requiring heap allocations, like vectors or hash maps,as well as anything that requires non-const function calls to be computed.
1.40.0+
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.
lazy-static.rs is available on crates.io.It is recommended to look there for the newest released version, as well as links to the newest builds of the docs.
At the point of the last update of this README, the latest published version could be used like this:
Add the following dependency to your Cargo manifest...
[dependencies]lazy_static ="1.5.0"
...and see thedocs for how to use it.
use lazy_static::lazy_static;use std::collections::HashMap;lazy_static!{static refHASHMAP:HashMap<u32,&'staticstr> ={letmut m =HashMap::new(); m.insert(0,"foo"); m.insert(1,"bar"); m.insert(2,"baz"); m};}fnmain(){// First access to `HASHMAP` initializes itprintln!("The entry for `0` is\"{}\".",HASHMAP.get(&0).unwrap());// Any further access to `HASHMAP` just returns the computed valueprintln!("The entry for `1` is\"{}\".",HASHMAP.get(&1).unwrap());}
It is now possible to easily replicate this crate's functionality in Rust's standard library withstd::sync::LazyLock
. The example above could also be written as:
use std::collections::HashMap;use std::sync::LazyLock;staticHASHMAP:LazyLock<HashMap<u32,&str>> =LazyLock::new(||{letmut m =HashMap::new(); m.insert(0,"foo"); m.insert(1,"bar"); m.insert(2,"baz"); m});fnmain(){// First access to `HASHMAP` initializes itprintln!("The entry for `0` is\"{}\".",HASHMAP.get(&0).unwrap());// Any further access to `HASHMAP` just returns the computed valueprintln!("The entry for `1` is\"{}\".",HASHMAP.get(&1).unwrap());}
Licensed under either of
- Apache License, Version 2.0, (LICENSE-APACHE orhttps://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT orhttps://opensource.org/licenses/MIT)
at your option.
Unless you explicitly state otherwise, any contribution intentionally submittedfor inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without anyadditional terms or conditions.
About
A small macro for defining lazy evaluated static variables in Rust.
Resources
License
Apache-2.0, MIT licenses found
Licenses found
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.