我对 Rust 很陌生,面临以下简单问题
我有以下 2 个枚举:
enum SourceType{
File,
Network
}
enum SourceProperties{
FileProperties {
file_path: String
},
NetworkProperties {
ip: String
}
}
现在我想拥有HashMap<SourceType, SourceProperties>,但在这样的实现中,可能会有映射File -> NetworkProperties,这不是预期的。
我正在考虑enum SourceProperties<T>以SourceType某种方式参数化,但这似乎是不可能的。有没有办法提供这样的类型安全保证?
UPD:的目的enum SourceType是实际SourceType是一个用户输入,它将被解码为一个String值("File", "Network")。所以工作流程看起来像这样
"File" -> SourceType::File -> SourceProperties::NetworkProperties