11lazy-static.rs
22==============
33
4- [ ![ 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 )
5-
64A macro for declaring lazily evaluated statics in Rust.
75
86Using this macro, it is possible to have ` static ` s that require code to be
97executed at runtime in order to be initialized.
108This includes anything requiring heap allocations, like vectors or hash maps,
11- as well as anything that requires function calls to be computed.
12-
13- # Syntax
14-
15- ``` rust
16- lazy_static! {
17- [pub ] static ref NAME_1 : TYPE_1 = EXPR_1 ;
18- [pub ] static ref NAME_2 : TYPE_2 = EXPR_2 ;
19- ...
20- [pub ] static ref NAME_N : TYPE_N = EXPR_N ;
21- }
22- ```
23-
24- # Semantic
25-
26- For a given ` static ref NAME: TYPE = EXPR; ` , the macro generates a
27- unique type that implements ` Deref<TYPE> ` and stores it in a static with name ` NAME ` .
9+ as well as anything that requires non-const function calls to be computed.
2810
29- On first deref, ` EXPR ` gets evaluated and stored internally, such that all further derefs
30- can return a reference to the same object.
31-
32- Like regular ` static mut ` s, this macro only works for types that fulfill the ` Sync `
33- trait.
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 )
3412
3513# Getting Started
3614
3715[ lazy-static.rs is available on crates.io] ( https://crates.io/crates/lazy_static ) .
38- Add the following dependency to your Cargo manifest to get the latest version of the 0.1 branch:
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...
3921
4022``` toml
4123[dependencies ]
42- lazy_static = " 0.1.* "
24+ lazy_static = " 0.2 "
4325```
4426
45- To always get the latest version, add this git repository to your
46- Cargo manifest:
27+ ...and see the [ docs] ( http://rust-lang-nursery.github.io/lazy-static.rs/lazy_static/index.html ) for how to use it.
4728
48- ``` toml
49- [dependencies .lazy_static ]
50- git = " https://github.com/rust-lang-nursery/lazy-static.rs"
51- ```
5229# Example
5330
54- Using the macro:
55-
5631``` rust
5732#[macro_use]
5833extern crate lazy_static;
@@ -67,15 +42,13 @@ lazy_static! {
6742 m . insert (2 , " baz" );
6843 m
6944 };
70- static ref COUNT : usize = HASHMAP . len ();
71- static ref NUMBER : u32 = times_two (21 );
7245}
7346
74- fn times_two (n : u32 ) -> u32 { n * 2 }
75-
7647fn main () {
77- println! ( " The map has {} entries. " , * COUNT );
48+ // First access to `HASHMAP` initializes it
7849 println! (" The entry for `0` is \ " {}\ " ." , HASHMAP . get (& 0 ). unwrap ());
79- println! (" An expensive calculation on a static results in: {}." , * NUMBER );
50+
51+ // Any further access to `HASHMAP` just returns the computed value
52+ println! (" The entry for `1` is \ " {}\ " ." , HASHMAP . get (& 1 ). unwrap ());
8053}
8154```
0 commit comments