diff --git a/src/structs/field-shorthand.md b/src/structs/field-shorthand.md index a3830d6b..6828ad59 100644 --- a/src/structs/field-shorthand.md +++ b/src/structs/field-shorthand.md @@ -26,13 +26,44 @@ fn main() { * The `new` function could be written using `Self` as a type, as it is interchangeable with the struct type name -```rust,ignore -impl Person { - fn new(name: String, age: u8) -> Self { - Self { name, age } - } -} -``` + ```rust,editable + #[derive(Debug)] + struct Person { + name: String, + age: u8, + } + impl Person { + fn new(name: String, age: u8) -> Self { + Self { name, age } + } + } + ``` +* Implement the `Default` trait for the struct. Define some fields and use the default values for the other fields. + + ```rust,editable + #[derive(Debug)] + struct Person { + name: String, + age: u8, + } + impl Default for Person { + fn default() -> Person { + Person { + name: "Bot".to_string(), + age: 0, + } + } + } + fn create_default() { + let tmp = Person { + ..Default::default() + }; + let tmp = Person { + name: "Sam".to_string(), + ..Default::default() + }; + } + ``` * Methods are defined in the `impl` block. * Use struct update syntax to define a new structure using `peter`. Note that the variable `peter` will no longer be accessible afterwards.