有没有更快/更短的方法来初始化 Rust 结构中的变量? [英] Is there a faster/shorter way to initialize variables in a Rust struct?
问题描述
在下面的示例中,我更愿意在字段声明中为结构中的每个字段分配一个值.或者,它实际上需要为每个字段添加一个语句来为这些字段分配一个值.我想要做的就是在结构体实例化时分配默认值.
In the following example, I would much prefer to assign a value to each field in the struct in the declaration of the fields. Alternatively, it effectively takes one additional statement for each field to assign a value to the fields. All I want to be able to do is to assign default values when the struct is instantiated.
有没有更简洁的方法来做到这一点?
Is there a more succinct way of doing this?
struct cParams {
iInsertMax: i64,
iUpdateMax: i64,
iDeleteMax: i64,
iInstanceMax: i64,
tFirstInstance: bool,
tCreateTables: bool,
tContinue: bool,
}
impl cParams {
fn new() -> cParams {
cParams {
iInsertMax: -1,
iUpdateMax: -1,
iDeleteMax: -1,
iInstanceMax: -1,
tFirstInstance: false,
tCreateTables: false,
tContinue: false,
}
}
}
推荐答案
你可以通过实现 Default
trait 来为你的结构提供默认值.default
函数看起来像您当前的 new
函数:
You can provide default values for your struct by implementing the Default
trait. The default
function would look like your current new
function:
impl Default for cParams {
fn default() -> cParams {
cParams {
iInsertMax: -1,
iUpdateMax: -1,
iDeleteMax: -1,
iInstanceMax: -1,
tFirstInstance: false,
tCreateTables: false,
tContinue: false,
}
}
}
然后您可以通过仅提供非默认值来实例化结构:
You can then instantiate the struct by giving only the non-default values:
let p = cParams { iInsertMax: 10, ..Default::default() };
通过对数据结构进行一些细微更改,您可以利用自动派生的默认实现.如果您在数据结构上使用 #[derive(Default)]
,编译器将自动为您创建一个默认函数,用其默认值填充每个字段.默认布尔值为 false,默认整数值为 0.
With some minor changes to your data structure, you can take advantage of an automatically derived default implementation. If you use #[derive(Default)]
on a data structure, the compiler will automatically create a default function for you that fills each field with its default value. The default boolean value is false, the default integral value is 0.
整数的默认值为 0 是一个问题,因为您希望整数字段默认为 -1.您可以定义一个实现默认值 -1 的新类型,并在结构中使用它代替 i64
.(我还没有测试过,但它应该可以工作).
An integer's default value being 0 is a problem here since you want the integer fields to be -1 by default. You could define a new type that implements a default value of -1 and use that instead of i64
in your struct. (I haven't tested that, but it should work).
但是,我建议稍微更改您的数据结构并使用 Option
而不是 i64
.我不知道您的代码的上下文,但看起来您正在使用 -1 的特殊值来表示无限"或没有最大值"的特殊含义.在 Rust 中,我们使用 Option
来表示可选的当前值.不需要 -1 hack.一个选项可以是 None
或 Some(x)
,其中 x 是你的 i64
.如果 -1 是唯一的负值,它甚至可能是一个无符号整数.默认的 Option
值为 None
,因此根据建议的更改,您的代码可能如下所示:
However, I'd suggest to slightly change your data structure and use Option<i64>
instead of i64
. I don't know the context of your code, but it looks like you're using the special value of -1 to represent the special meaning "infinite", or "there's no max". In Rust, we use an Option
to represent an optionally present value. There's no need for a -1 hack. An option can be either None
or Some(x)
where x would be your i64
here. It might even be an unsigned integer if -1 was the only negative value. The default Option
value is None
, so with the proposed changes, your code could look like this:
#[derive(Default)]
struct cParams {
iInsertMax: Option<u64>,
iUpdateMax: Option<u64>,
iDeleteMax: Option<u64>,
iInstanceMax: Option<u64>,
tFirstInstance: bool,
tCreateTables: bool,
tContinue: bool,
}
let p = cParams { iInsertMax: Some(10), ..Default::default() };
这篇关于有没有更快/更短的方法来初始化 Rust 结构中的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!