【发布时间】:2020-07-06 15:14:29
【问题描述】:
我有代码 (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.
}
在需要引用项目时不断输入"a"、"b"等是不好的。
一个枚举可以用来改善这个,比如:
enum Items {
A = SomeStruct { x: 2., y: 2 },
B = SomeStruct { x: 3.5, y: 1 },
C = SomeStruct { x: 0., y: 5 },
}
这实际上是一个const 枚举,这样当将来引用这些项目时,我们可以简单地使用Items::A 或&Items::A 而不是'a',并且必须进行必要的哈希处理。
看来这样做是不可行的。
有没有办法使用const enum?还是有其他更好的解决方案?
虽然这个问题可能与How can I create enums with constant values in Rust? 重复,但在使用任意结构时,根据该问题提出的解决方案不起作用。 solution vallentin added 确实如此,但此解决方案确实更适用于其他解决方案不起作用的情况。我认为它在这个问题的上下文中提供了一个更好更清晰的答案,而其他更简单的解决方案不起作用。
【问题讨论】:
-
accepted answer is "match in a function",在这个接受的答案中,enum_map solution works for your case,以及相关的 const 答案 - 4 个答案中有 3 个适用于您的案例,只有特定于原语的答案不工作。