I want to alloc static hashmap,but ig gives me error "calls in statics are limited to constant functions, tuple structs and tuple variants".
How can i fix this error?
Asked
Active
Viewed 294 times
-2

AlePon
- 15
- 2
-
3Don't paste an image of your code but your code itself (and optionally a link to a playground exhibit) ! – Denys Séguret Aug 02 '21 at 10:31
-
2Does this answer your question? [How do I create a global, mutable singleton?](https://stackoverflow.com/questions/27791532/how-do-i-create-a-global-mutable-singleton) – Elias Holzmann Aug 02 '21 at 10:36
1 Answers
0
You may want to use lazy_static using a RwLock (as you seem to update the content dynamically).
RwLock allow you to read and write to the HashMap without the need of a Mutex, this in fact allow you to have multiple reader (but only one writer). If your program is not async, then just leave the HashMap as mut in the lazy_static. A Mutex is also a good option, but it only allow you to have one reader at a time, do some bench for your program and chose the best solution for you.
Example of a lazy_static Vec with RwLock here.

Martichou
- 123
- 1
- 10