this post was submitted on 25 Jun 2023
6 points (100.0% liked)
Rust Programming
8144 readers
78 users here now
founded 5 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
You may be able to use something like lazy_static.
https://docs.rs/lazy_static/latest/lazy_static/
I heard about this, but I wasn't sure it was the right way. Or if Rust developers just straight up avoid situations like this.
lazy_static was the standard way as far as I'm concerned until this month. OnceCell or OnceLock should fill this role now.
I generally avoid this situation. At best I'll create an Rc<HashMap<T, U>> to pass around. I find that having a need for a static variable can be an indication of bad design. It often makes the code that depends on it untestable.