Creating lazy static objects
Big objects, especially constant ones, should be reused instead of rebuilt. The lazy_static!
macro helps you with this by extending Rust's normal static
functionality, which normally requires your objects to be constructable at compile-time, with the ability to create lazy objects that are initialized during runtime.
How to do it...
- Create a Rust project to work on during this chapter with
cargo new chapter_five
. - Navigate into the newly created
chapter_five
folder. For the rest of this chapter, we will assume that your command line is currently in this directory. - Inside the
src
folder, create a new folder calledbin
. - Delete the generated
lib.rs
file, as we are not creating a library. Open the
Cargo.toml
file that was generated earlier for you.- Under
[dependencies]
, add the following lines:
lazy_static = "1.0" regex = "0.2"
If you want, you can go to the crates.io web pages for lazy_static
(https://crates.io/crates/lazy_static) and regex (https://crates.io/crates/regex...