【发布时间】:2018-07-01 12:37:55
【问题描述】:
来自std::default::Default 文档:
#[derive(Default)]
struct SomeOptions {
foo: i32,
bar: f32,
}
fn main() {
let options = SomeOptions { foo: 42, ..Default::default() };
}
.. 前缀对Default::default() 的返回值有什么作用,为什么这里有必要?它几乎看起来像是在充当传播运算符,但我不确定。我了解..Default::default() 在做什么——用SomeOptions 的默认值填充剩余的结构参数,但不知道.. 的工作原理。这个运营商叫什么名字?
【问题讨论】:
-
另请参阅What does the double-ellipsis operator mean in the context of a &str subscript?,了解有关
..在 Rust 中的另一种用法的信息。
标签: rust default operator-keyword