1
1
lazy-static.rs
2
2
==============
3
3
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
-
6
4
A macro for declaring lazily evaluated statics in Rust.
7
5
8
6
Using this macro, it is possible to have ` static ` s that require code to be
9
7
executed at runtime in order to be initialized.
10
8
This 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.
28
10
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 )
34
12
35
13
# Getting Started
36
14
37
15
[ 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...
39
21
40
22
``` toml
41
23
[dependencies ]
42
- lazy_static = " 0.1.* "
24
+ lazy_static = " 0.2 "
43
25
```
44
26
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.
47
28
48
- ``` toml
49
- [dependencies .lazy_static ]
50
- git = " https://github.com/rust-lang-nursery/lazy-static.rs"
51
- ```
52
29
# Example
53
30
54
- Using the macro:
55
-
56
31
``` rust
57
32
#[macro_use]
58
33
extern crate lazy_static;
@@ -67,15 +42,13 @@ lazy_static! {
67
42
m . insert (2 , " baz" );
68
43
m
69
44
};
70
- static ref COUNT : usize = HASHMAP . len ();
71
- static ref NUMBER : u32 = times_two (21 );
72
45
}
73
46
74
- fn times_two (n : u32 ) -> u32 { n * 2 }
75
-
76
47
fn main () {
77
- println! ( " The map has {} entries. " , * COUNT );
48
+ // First access to `HASHMAP` initializes it
78
49
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 ());
80
53
}
81
54
```
0 commit comments