自动为结构实例分配唯一的随机 ID

发布于 2025-01-17 06:18:54 字数 1652 浏览 4 评论 0原文

全新的锈类动物。假设我有这段代码:

use random_string;

struct IDManager {
    length: usize,
    charset: String,
    registered_ids: Vec<String>,
}

impl IDManager {
    fn generate(&mut self) -> &String {
        loop {
            let id = random_string::generate(
                self.length, 
                &self.charset
            );
            if !self.registered_ids.contains(&id) {
                self.registered_ids.push(id);
                return self.registered_ids.last().unwrap();
            }
        }
    }
}

impl Default for IDManager{
    fn default() -> Self {
        IDManager { 
            length: 32, 
            registered_ids: vec![],
            charset: [
                "abcdefghijklmnopqrstuvwxyz",
                "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
                "0123456789",
            ].concat(), 
        }
    }
}

#[derive(Debug)]
struct Person<'a> {
    id: &'a String,
    name: String,
    age: u32,
}

fn main() {
    let mut person_id_manager: IDManager = Default::default();
    let person = Person {
        id: person_id_manager.generate(),
        name: "Bob".to_string(),
        age: 32,
    };
    println! ("{:?}", person)    
}

这段代码的问题是,person_id_manager 并没有真正绑定到struct Person。用户仍然可以自由地向 Person 提供他们想要的任何 id,但这不是我们想要的。相反,理想情况下,我们不希望用户控制如何生成 Personid,最好是完全不可见。

我想到了在Person的“类”中添加一个IDManager“静态属性”,而Person显然不是,因为它是一个>结构。在 Rust 中执行此操作的惯用方法是什么?

Completely new rustacean. Suppose I have this piece of code:

use random_string;

struct IDManager {
    length: usize,
    charset: String,
    registered_ids: Vec<String>,
}

impl IDManager {
    fn generate(&mut self) -> &String {
        loop {
            let id = random_string::generate(
                self.length, 
                &self.charset
            );
            if !self.registered_ids.contains(&id) {
                self.registered_ids.push(id);
                return self.registered_ids.last().unwrap();
            }
        }
    }
}

impl Default for IDManager{
    fn default() -> Self {
        IDManager { 
            length: 32, 
            registered_ids: vec![],
            charset: [
                "abcdefghijklmnopqrstuvwxyz",
                "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
                "0123456789",
            ].concat(), 
        }
    }
}

#[derive(Debug)]
struct Person<'a> {
    id: &'a String,
    name: String,
    age: u32,
}

fn main() {
    let mut person_id_manager: IDManager = Default::default();
    let person = Person {
        id: person_id_manager.generate(),
        name: "Bob".to_string(),
        age: 32,
    };
    println! ("{:?}", person)    
}

The problem with this code is that the person_id_manager is not really tied to the struct Person. Users are still free to give whatever id they want to the Person which is not what we want. Instead, ideally, we want to yield no control to the users how the id of a Person is generated, better if it is completely invisible.

I thought of adding a IDManager "static attribute" to the "class" of Person, which Person is obviously not because it is a struct. What is the idiomatic way to do this in Rust?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寒尘 2025-01-24 06:18:54

通常在 Rust 中,您可以通过不公开字段来限制对字段的访问。在您的示例中,Person 的用户只能访问私有字段,因为您已将所有内容都放在一个模块中。

很难准确地说出什么是惯用的,因为它看起来像是一个稍微不寻常的要求,而“最佳”方法将取决于您真正想要实现的目标。然而,在 Rust 中,使用 new 方法来控制对象的构造方式是惯用的。

由于这些字段都是私有的,如果您提供一个公共的 new 方法,这将是可以在模块外部构造 Person 的唯一方法,因此您可以精确控制这是如何完成的。

此时,您可以使用 IdManager 的共享 static 实例,但由于您询问了惯用方法,并且通常不建议在任何语言中使用全局变量,所以让我们这样做有所不同:

impl<'a> Person<'a> {
    pub fn new(id_manager: &'a mut IdManager, name: String, age: u32) -> Self {
        Person {
            name,
            age,
            id: id_manager.generate()
        } 
    }
}

这需要 Person 结构的用户能够访问 IdManager 才能创建一个。

Usually in Rust, you limit access to fields by not making them public. In your example, the user of Person is only able to access the private fields because you've put everything in one module.

It's hard to say exactly what is idiomatic here, because it looks like a slightly unusual requirement and the "best" approach will depend on what you really are trying to achieve. However, it is idiomatic in Rust to use new method to control how objects are constructed.

Since the fields are all private, if you provide a public new method, this will be the only way that Person can be constructed outside of the module, so you can control exactly how this is done.

At this point you could make use of a shared static instance of IdManager, but since you asked about an idiomatic approach and global variables are not typically recommended in any language, let's do something different:

impl<'a> Person<'a> {
    pub fn new(id_manager: &'a mut IdManager, name: String, age: u32) -> Self {
        Person {
            name,
            age,
            id: id_manager.generate()
        } 
    }
}

This requires users of the Person struct to have access to an IdManager in order to create one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文