-2

I want to alloc static hashmap,but ig gives me error "calls in statics are limited to constant functions, tuple structs and tuple variants". enter image description here How can i fix this error?

AlePon
  • 15
  • 2
  • 3
    Don'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
  • 2
    Does 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 Answers1

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