I want to create a global hash map that maps strings to vectors of colors. This data needs to be queried by multiple functions and should just be hard coded into the program. That doesn’t seem possible.

Now, how is the right ™ way to do something like that in Rust? What if you need just a bunch of data structures from the beginning of the program until its end where some of the data needs to allocated?

  • SigmarSternOP
    link
    fedilink
    arrow-up
    3
    ·
    1 year ago

    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.

    • Nyefan@lemmy.ml
      link
      fedilink
      arrow-up
      4
      ·
      1 year ago

      lazy_static was the standard way as far as I’m concerned until this month. OnceCell or OnceLock should fill this role now.

    • livingcoder@lemmy.austinwadeheller.com
      link
      fedilink
      arrow-up
      3
      ·
      edit-2
      1 year ago

      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.