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

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

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&lt;&amp;str, MyStruct&gt; = [
    (&quot;Instance1&quot;, MyStruct {
        // fields...
    }),
    (&quot;Instance2&quot;, MyStruct {
        // fields...
    }),
    (&quot;Instance3&quot;, MyStruct {
        // fields...
    }),
    // Add more instances as needed
].iter().cloned().collect();

fn main() {
    let instance = INSTANCES.get(&quot;Instance1&quot;);
    // ...
}

Or this:

struct MyStruct {
    // ...
}

enum MyStructInstance {
    Instance1,
    Instance2,
    Instance3,
    // Add more instances as needed
}

impl MyStructInstance {
    fn get_instance(&amp;self) -&gt; MyStruct {
        match self {
            MyStructInstance::Instance1 =&gt; MyStruct {
                // fields...
            },
            MyStructInstance::Instance2 =&gt; MyStruct {
                // fields...
            },
            MyStructInstance::Instance3 =&gt; 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.

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:

确定