Creating lazily evaluated statics

We have seen in previous chapters how, in nightly Rust, it is possible to call some trivial constant functions that are evaluated at compile time. Nevertheless, this might not be enough for our needs, and we might not even want to use nightly Rust.

In this case, we can use a great crate, and the macro with the same name—lazy_static. This macro allows us to create static variables that will run the code to be generated on their first use. Let's check it, for example, for a HashMap. Creating a HashMap or adding values to it cannot be done during compile time. As we saw in previous chapters, this can be improved by using the phf crate. But what if we want to add values to the HashMap based on some environment ...

Get Rust High Performance now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.