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_fivefolder. For the rest of this chapter, we will assume that your command line is currently in this directory. - Inside the
srcfolder, create a new folder calledbin. - Delete the generated
lib.rsfile, as we are not creating a library. Open the
Cargo.tomlfile 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...