英文:
How to store predefined instances of a struct in a way that they can be referenced by name?
问题
我想存储许多预定义实例的结构体作为常量,并以一种能够通过名称引用它们的方式进行分组。
我可以这样实现:
use std::collections::HashMap;
struct MyStruct {
// ...
}
const INSTANCES: HashMap<&str, MyStruct> = [
("Instance1", MyStruct {
// fields...
}),
("Instance2", MyStruct {
// fields...
}),
("Instance3", MyStruct {
// fields...
}),
// 添加更多实例,根据需要
].iter().cloned().collect();
fn main() {
let instance = INSTANCES.get("Instance1");
// ...
}
或者这样:
struct MyStruct {
// ...
}
enum MyStructInstance {
Instance1,
Instance2,
Instance3,
// 根据需要添加更多实例
}
impl MyStructInstance {
fn get_instance(&self) -> MyStruct {
match self {
MyStructInstance::Instance1 => MyStruct {
// fields...
},
MyStructInstance::Instance2 => MyStruct {
// fields...
},
MyStructInstance::Instance3 => MyStruct {
// fields...
},
// ...
}
}
}
fn main() {
let instance1 = MyStructInstance::Instance1.get_instance();
// ...
}
但似乎不够规范。存储预定义实例的命名列表的标准方式是什么?
英文:
I would like to store many predefined instances of a struct as constants and group them in a way that I am able to reference them by name.
I could achieve it like this:
use std::collections::HashMap;
struct MyStruct {
// ...
}
const INSTANCES: HashMap<&str, MyStruct> = [
("Instance1", MyStruct {
// fields...
}),
("Instance2", MyStruct {
// fields...
}),
("Instance3", MyStruct {
// fields...
}),
// Add more instances as needed
].iter().cloned().collect();
fn main() {
let instance = INSTANCES.get("Instance1");
// ...
}
Or this:
struct MyStruct {
// ...
}
enum MyStructInstance {
Instance1,
Instance2,
Instance3,
// Add more instances as needed
}
impl MyStructInstance {
fn get_instance(&self) -> MyStruct {
match self {
MyStructInstance::Instance1 => MyStruct {
// fields...
},
MyStructInstance::Instance2 => MyStruct {
// fields...
},
MyStructInstance::Instance3 => MyStruct {
// fields...
},
// ...
}
}
}
fn main() {
let instance1 = MyStructInstance::Instance1.get_instance();
// ...
}
But it does not seem well formed. What is the standard way for storing a named list of predefined instances?
答案1
得分: 1
我想将许多预定义的结构实例存储为 常量 并以一种方式 分组,以便我能够通过 名称 对它们进行 引用。
pub mod group {
pub struct MyStruct {
// ...
}
pub const Instance1: MyStruct = MyStruct {};
pub const Instance2: MyStruct = MyStruct {};
pub const Instance3: MyStruct = MyStruct {};
}
fn main() {
let instance1 = group::Instance1;
let instance2 = group::Instance2;
// ...
}
英文:
> I would like to store many predefined instances of a struct as constants and group them in a way that I am able to reference them by name.
pub mod group {
pub struct MyStruct {
// ...
}
pub const Instance1: MyStruct = MyStruct {};
pub const Instance2: MyStruct = MyStruct {};
pub const Instance3: MyStruct = MyStruct {};
}
fn main() {
let instance1 = group::Instance1;
let instance2 = group::Instance2;
// ...
}
Playground. If you want to modify them, you can use static
with unsafe
updates. You may implement FromStr
trait for MyStruct
, if you need conversions from strings.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论