-
Book Overview & Buying
-
Table Of Contents
Design Patterns and Best Practices in Rust
By :
In this section, we'll discuss one more way that people learning Rust try to outwit the borrow checker: tricks involving global state and mutable static variables. As with many of the topics we've discussed, these absolutely have their uses. But using global statics reflexively to avoid borrow checker issues is not the answer it may seem to be.
As we have seen before, managing the flow of data is critical for working with Rust effectively. It can sometimes be difficult to manage references across a substantial code base. It can be even more difficult when some of that state needs to be mutable. The borrow checker will often express opinions on the matter, and care is needed to be able to manage mutable references throughout your code.
But what if there was a way to bypass the whole issue?
One very common solution for people early in their Rust journey is to turn to global static variables. Crates such as LazyStatic and OnceCell...