How do I solve the problem of lack of "cache" in Rust?

I have a problem that I can't really solve at all. I need to store certain function pointers in a single point so that they can be used later by other functions. But the problem itself is that there is no support for global variables in rust. I read about the reason for this lack and I agree that it is less subject to bugs, so but taking it out entirely forces you to keep a copy of these pointers per object. static is very limited for practical use, it can only be used with primitive types or with functions that are const. Is there any way to create a "cache" per thread or global to hold objects?
[Automatic translation]

A typical solution is to store a Mutex in a global static once_cell::sync::Lazy.

(This will probably be added to the standard library in the future.)

You can also use RefCell and thread_local! to create a per-thread cache.

This crate is incredible, that's what I was looking for, thanks!

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.