From 039da2ab3f2314ce7ef1cddef18ee205fb7fcc29 Mon Sep 17 00:00:00 2001 From: Amanda Graven Date: Fri, 28 Nov 2025 10:30:40 +0100 Subject: [PATCH] Add examples for default field values --- src/custom_types/structs.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/custom_types/structs.md b/src/custom_types/structs.md index eccd30eb2d..8e86831947 100644 --- a/src/custom_types/structs.md +++ b/src/custom_types/structs.md @@ -37,6 +37,12 @@ struct Rectangle { bottom_right: Point, } +// Struct with named fields can have default values +struct Command { + command: String, + arguments: Vec = Vec::new(), +} + fn main() { // Create struct with field init shorthand let name = String::from("Peter"); @@ -70,6 +76,9 @@ fn main() { bottom_right: bottom_right, }; + // Instantiate a `Command` with only required fields specified + let command = Command { command: String::from("ls"), .. }; + // Instantiate a unit struct let _unit = Unit;