如何以一种可以按名称引用的方式存储结构体的预定义实例?

huangapple go评论90阅读模式
英文:

How to store predefined instances of a struct in a way that they can be referenced by name?

问题

我想存储许多预定义实例的结构体作为常量,并以一种能够通过名称引用它们的方式进行分组。

我可以这样实现:

  1. use std::collections::HashMap;
  2. struct MyStruct {
  3. // ...
  4. }
  5. const INSTANCES: HashMap<&str, MyStruct> = [
  6. ("Instance1", MyStruct {
  7. // fields...
  8. }),
  9. ("Instance2", MyStruct {
  10. // fields...
  11. }),
  12. ("Instance3", MyStruct {
  13. // fields...
  14. }),
  15. // 添加更多实例,根据需要
  16. ].iter().cloned().collect();
  17. fn main() {
  18. let instance = INSTANCES.get("Instance1");
  19. // ...
  20. }

或者这样:

  1. struct MyStruct {
  2. // ...
  3. }
  4. enum MyStructInstance {
  5. Instance1,
  6. Instance2,
  7. Instance3,
  8. // 根据需要添加更多实例
  9. }
  10. impl MyStructInstance {
  11. fn get_instance(&self) -> MyStruct {
  12. match self {
  13. MyStructInstance::Instance1 => MyStruct {
  14. // fields...
  15. },
  16. MyStructInstance::Instance2 => MyStruct {
  17. // fields...
  18. },
  19. MyStructInstance::Instance3 => MyStruct {
  20. // fields...
  21. },
  22. // ...
  23. }
  24. }
  25. }
  26. fn main() {
  27. let instance1 = MyStructInstance::Instance1.get_instance();
  28. // ...
  29. }

但似乎不够规范。存储预定义实例的命名列表的标准方式是什么?

英文:

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:

  1. use std::collections::HashMap;
  2. struct MyStruct {
  3. // ...
  4. }
  5. const INSTANCES: HashMap&lt;&amp;str, MyStruct&gt; = [
  6. (&quot;Instance1&quot;, MyStruct {
  7. // fields...
  8. }),
  9. (&quot;Instance2&quot;, MyStruct {
  10. // fields...
  11. }),
  12. (&quot;Instance3&quot;, MyStruct {
  13. // fields...
  14. }),
  15. // Add more instances as needed
  16. ].iter().cloned().collect();
  17. fn main() {
  18. let instance = INSTANCES.get(&quot;Instance1&quot;);
  19. // ...
  20. }

Or this:

  1. struct MyStruct {
  2. // ...
  3. }
  4. enum MyStructInstance {
  5. Instance1,
  6. Instance2,
  7. Instance3,
  8. // Add more instances as needed
  9. }
  10. impl MyStructInstance {
  11. fn get_instance(&amp;self) -&gt; MyStruct {
  12. match self {
  13. MyStructInstance::Instance1 =&gt; MyStruct {
  14. // fields...
  15. },
  16. MyStructInstance::Instance2 =&gt; MyStruct {
  17. // fields...
  18. },
  19. MyStructInstance::Instance3 =&gt; MyStruct {
  20. // fields...
  21. },
  22. // ...
  23. }
  24. }
  25. }
  26. fn main() {
  27. let instance1 = MyStructInstance::Instance1.get_instance();
  28. // ...
  29. }

But it does not seem well formed. What is the standard way for storing a named list of predefined instances?

答案1

得分: 1

我想将许多预定义的结构实例存储为 常量 并以一种方式 分组,以便我能够通过 名称 对它们进行 引用

  1. pub mod group {
  2. pub struct MyStruct {
  3. // ...
  4. }
  5. pub const Instance1: MyStruct = MyStruct {};
  6. pub const Instance2: MyStruct = MyStruct {};
  7. pub const Instance3: MyStruct = MyStruct {};
  8. }
  9. fn main() {
  10. let instance1 = group::Instance1;
  11. let instance2 = group::Instance2;
  12. // ...
  13. }
英文:

> 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.

  1. pub mod group {
  2. pub struct MyStruct {
  3. // ...
  4. }
  5. pub const Instance1: MyStruct = MyStruct {};
  6. pub const Instance2: MyStruct = MyStruct {};
  7. pub const Instance3: MyStruct = MyStruct {};
  8. }
  9. fn main() {
  10. let instance1 = group::Instance1;
  11. let instance2 = group::Instance2;
  12. // ...
  13. }

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.

huangapple
  • 本文由 发表于 2023年5月18日 04:26:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76275960.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定