I have the code (playground):
use std::collections::HashMap;
// We have some arbitrary struct (given values just placeholders)
struct SomeStruct {
x: f32,
y: usize,
}
fn main() {
// We have some hashmap which contains the names (keys) and properties (values) of items.
// These are known at compile time.
let hashmap: HashMap<&str, SomeStruct> = vec![
("a", SomeStruct { x: 2., y: 2 }),
("b", SomeStruct { x: 3.5, y: 1 }),
("c", SomeStruct { x: 0., y: 5 }),
]
.into_iter()
.collect();
// We then have a bunch of data which arbitrarily references the names of these items.
// This data is also known at compile time.
}
Constantly typing "a"
,"b"
, etc when needing to refer to the items is bad.
An enum could possibly be used to make this better, something like:
enum Items {
A = SomeStruct { x: 2., y: 2 },
B = SomeStruct { x: 3.5, y: 1 },
C = SomeStruct { x: 0., y: 5 },
}
This would effectively be a const
enum, such that when referring to these items in the future we could simply do Items::A
or &Items::A
rather than 'a'
and having to do the necessary hashing.
It seems that doing this is not viable.
Is there a way to use a const enum
? Or is there an alternative better solution?
While this question may be a duplicate of How can I create enums with constant values in Rust? the solutions proposed under that question did not work when using an arbitrary struct. The solution vallentin added does but this solution really applies more to this circumstance where the other solutions do not work. I think it provides a better clearer answer in context of this question where the other simpler solutions do not work.