README.md
1An example of parsing a custom syntax within a `functionlike!(...)` procedural
2macro. Demonstrates how to trigger custom warnings and error messages on
3individual tokens of the input.
4
5- [`lazy-static/src/lib.rs`](lazy-static/src/lib.rs)
6- [`example/src/main.rs`](example/src/main.rs)
7
8The library implements a `lazy_static!` macro similar to the one from the real
9[`lazy_static`](https://docs.rs/lazy_static/1.0/lazy_static/) crate on
10crates.io.
11
12```rust
13lazy_static! {
14 static ref USERNAME: Regex = Regex::new("^[a-z0-9_-]{3,16}$").unwrap();
15}
16```
17
18Compile and run the example by doing `cargo run` in the directory of the
19`example` crate.
20
21The implementation shows how to trigger custom warnings and error messages on
22the macro input. For example if you try adding an uncreatively named `FOO` lazy
23static, the macro will scold you with the following warning.
24
25```
26warning: come on, pick a more creative name
27 --> src/main.rs:10:16
28 |
2910 | static ref FOO: String = "lazy_static".to_owned();
30 | ^^^
31```
32
33And if you try to lazily initialize `() = ()`, the macro will outright refuse to
34compile it for you.
35
36```
37error: I can't think of a legitimate use for lazily initializing the value `()`
38 --> src/main.rs:10:27
39 |
4010 | static ref UNIT: () = ();
41 | ^^
42```
43