Skip to content

Commit 580f118

Browse files
Add examples for default field values
1 parent b656f3c commit 580f118

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

src/custom_types/structs.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ struct Rectangle {
3737
bottom_right: Point,
3838
}
3939
40+
// Struct with named fields can have default values
41+
struct Command {
42+
command: String,
43+
arguments: Vec<String> = Vec::new(),
44+
}
45+
4046
fn main() {
4147
// Create struct with field init shorthand
4248
let name = String::from("Peter");
@@ -70,6 +76,9 @@ fn main() {
7076
bottom_right: bottom_right,
7177
};
7278
79+
// Instantiate a `Command` with only required fields specified
80+
let command = Command { command: String::from("ls"), .. };
81+
7382
// Instantiate a unit struct
7483
let _unit = Unit;
7584

0 commit comments

Comments
 (0)