Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Rust Standard Library Cookbook

You're reading from   Rust Standard Library Cookbook Over 75 recipes to leverage the power of Rust

Arrow left icon
Product type Paperback
Published in Mar 2018
Publisher Packt
ISBN-13 9781788623926
Length 360 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
Jan Hohenheim Jan Hohenheim
Author Profile Icon Jan Hohenheim
Jan Hohenheim
Daniel Durante Daniel Durante
Author Profile Icon Daniel Durante
Daniel Durante
Arrow right icon
View More author details
Toc

Table of Contents (12) Chapters Close

Preface 1. Learning the Basics FREE CHAPTER 2. Working with Collections 3. Handling Files and the Filesystem 4. Serialization 5. Advanced Data Structures 6. Handling Errors 7. Parallelism and Rayon 8. Working with Futures 9. Networking 10. Using Experimental Nightly Features 11. Other Books You May Enjoy

How it works...

By invoking the lazy_static! macro [10, 21 and 29], we define a lazily initialized object in the current scope. Lazy here means created only the first time it is used.

Contrary to a let binding, its scope can also be the global scope [10]. A realistic example for this is creating a collection with a known content that is used by many functions, as the alternative would be to create it once and pass it around endlessly.

If your lazy_static consists of a Vec with content that is known at compile time, you can instead use a const array, as its construction is constant. In terms of code, this means you don't need to use this:
lazy_static!{
static ref FOO: Vec<&'static str> = vec!["a", "b", "c"];
}
Instead, you can use the following:
const FOO: [&str; 3] = ["a", "b", "c"];

Remember when, in Chapter 1, Learning the Basics; Querying with Regexes, we talked about how compiling Regexes is...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime