Default
特徵
Default
特徵會產生型別的預設值。
Speaker Notes
This slide should take about 5 minutes.
- 這可以直接實作,也可以透過
#[derive(Default)]
衍生得出。 - A derived implementation will produce a value where all fields are set to their default values.
- 也就是說,該結構體中的所有型別也都必須實作
Default
。
- 也就是說,該結構體中的所有型別也都必須實作
- 標準的 Rust 型別通常會以合理的值 (例如
0
、""
等等) 實作Default
。 - The partial struct initialization works nicely with default.
- The Rust standard library is aware that types can implement
Default
and provides convenience methods that use it. - The
..
syntax is called struct update syntax.