I'm assuming the answer is "No", because it seems like you can't do any allocation to set up a constant. What's the best way to do this/approximate this? I was thinking something super ugly like creating a struct that has a get_map_const() function that returns the 'constant' value. Is there a cleaner alternative?
(Note that I can't avoid the need for a map constant - but I can come up with different representations for it.)
You mean, a static HashMap? No, it isn't possible, because Rust does not (yet) support compile-time evaluation of code of such complexity as would be needed to implement a hashmap.
What are you trying to do that requires a static hashmap?
Ah. I figured. And no - haven't taken a look at phf; will look tonight. As for what I'm writing: it's a code-generator to a spec, and the spec defines map literals that have to be translated to the target language.
They are about to release phf 0.8.0 to crates.io, which will feature, among other things, macros working on stable Rust. If you just want the stable macros, you can currently get them by putting
No need to answer to a 5 years old topic, 2 years after the last post with an obviously unsound (since it isn’t thread-safe) new solution
Also, I would recommend anyone who isn’t at least somewhat decently familiar with Rust to avoid usage of unsafe in general, if possible. (I’m taking things like unnecessarily boxing of String as an indicator for the level language familiarity here.)