知识卡片
默认实例可作字段更新模板
Default::default()提供默认实例,结构体初始化中的..Default::default()从中取得未显式填写的字段,按字段语义移动或复制。它让可选设置集中,但默认值应真实合理;自动派生要求组成满足条件,枚举也需标明适用的默认变体。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Chapter 2. Traits Default · 查看原书依据
struct update syntax. This syntax allows struct fields to be initialized by copying or moving their contents from an existing instance of the same struct, for any fields that aren’t explicitly initialized. The template to copy from is given at the end of the initialization, after .., and the Default trait provides an ideal template to use: #[derive(Default)]
参考来源
- 位置:《Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)》第2章《Default》(spine6;extracted/Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)/_epub-src/OEBPS/ch02.html;u059-ch02-005)。
- 结论依据:原文在“struct update syntax”附近的定义、例子或代码支撑本卡核心说明:Default::default()提供默认实例,结构体初始化中的..Default::default()从中取得未显式填写的字段,按字段语义移动或复制。边界按已读前提解释;示例未执行。
- 原始内容:struct update syntax. This syntax allows struct fields to be initialized by copying or moving their contents from an existing instance of the same struct, for any fields that aren’t explicitly initialized. The template to copy from is given at the end of the initialization, after .., and the Default trait provides an ideal template to use: #[derive(Default)]