自动为结构实例分配唯一的随机 ID
全新的锈类动物。假设我有这段代码:
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
,但这不是我们想要的。相反,理想情况下,我们不希望用户控制如何生成 Person
的 id
,最好是完全不可见。
我想到了在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通常在 Rust 中,您可以通过不公开字段来限制对字段的访问。在您的示例中,
Person
的用户只能访问私有字段,因为您已将所有内容都放在一个模块中。很难准确地说出什么是惯用的,因为它看起来像是一个稍微不寻常的要求,而“最佳”方法将取决于您真正想要实现的目标。然而,在 Rust 中,使用 new 方法来控制对象的构造方式是惯用的。
由于这些字段都是私有的,如果您提供一个公共的 new 方法,这将是可以在模块外部构造
Person
的唯一方法,因此您可以精确控制这是如何完成的。此时,您可以使用
IdManager
的共享static
实例,但由于您询问了惯用方法,并且通常不建议在任何语言中使用全局变量,所以让我们这样做有所不同:这需要
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 thatPerson
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 ofIdManager
, but since you asked about an idiomatic approach and global variables are not typically recommended in any language, let's do something different:This requires users of the
Person
struct to have access to anIdManager
in order to create one.