From 18267aabcf8a87d90ac37018bee73685f8ea59e5 Mon Sep 17 00:00:00 2001 From: Amanda Graven Date: Fri, 28 Nov 2025 17:11:22 +0100 Subject: [PATCH] Add section about default field values to ch05-01 --- .../Cargo.lock | 5 +++ .../Cargo.toml | 6 ++++ .../src/main.rs | 10 ++++++ .../Cargo.lock | 5 +++ .../Cargo.toml | 6 ++++ .../src/main.rs | 23 ++++++++++++ src/ch05-01-defining-structs.md | 36 +++++++++++++++++-- 7 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 listings/ch05-using-structs-to-structure-related-data/no-listing-07-default-field-value/Cargo.lock create mode 100644 listings/ch05-using-structs-to-structure-related-data/no-listing-07-default-field-value/Cargo.toml create mode 100644 listings/ch05-using-structs-to-structure-related-data/no-listing-07-default-field-value/src/main.rs create mode 100644 listings/ch05-using-structs-to-structure-related-data/no-listing-08-applying-defaults/Cargo.lock create mode 100644 listings/ch05-using-structs-to-structure-related-data/no-listing-08-applying-defaults/Cargo.toml create mode 100644 listings/ch05-using-structs-to-structure-related-data/no-listing-08-applying-defaults/src/main.rs diff --git a/listings/ch05-using-structs-to-structure-related-data/no-listing-07-default-field-value/Cargo.lock b/listings/ch05-using-structs-to-structure-related-data/no-listing-07-default-field-value/Cargo.lock new file mode 100644 index 0000000000..5804b18024 --- /dev/null +++ b/listings/ch05-using-structs-to-structure-related-data/no-listing-07-default-field-value/Cargo.lock @@ -0,0 +1,5 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "structs" +version = "0.1.0" diff --git a/listings/ch05-using-structs-to-structure-related-data/no-listing-07-default-field-value/Cargo.toml b/listings/ch05-using-structs-to-structure-related-data/no-listing-07-default-field-value/Cargo.toml new file mode 100644 index 0000000000..7251aaa8de --- /dev/null +++ b/listings/ch05-using-structs-to-structure-related-data/no-listing-07-default-field-value/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "structs" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/listings/ch05-using-structs-to-structure-related-data/no-listing-07-default-field-value/src/main.rs b/listings/ch05-using-structs-to-structure-related-data/no-listing-07-default-field-value/src/main.rs new file mode 100644 index 0000000000..16c9df0ba5 --- /dev/null +++ b/listings/ch05-using-structs-to-structure-related-data/no-listing-07-default-field-value/src/main.rs @@ -0,0 +1,10 @@ +// ANCHOR: here +struct User { + active: bool = true, + username: String, + email: String, + sign_in_count: u64 = 1, +} +// ANCHOR_END: here + +fn main() {} diff --git a/listings/ch05-using-structs-to-structure-related-data/no-listing-08-applying-defaults/Cargo.lock b/listings/ch05-using-structs-to-structure-related-data/no-listing-08-applying-defaults/Cargo.lock new file mode 100644 index 0000000000..5804b18024 --- /dev/null +++ b/listings/ch05-using-structs-to-structure-related-data/no-listing-08-applying-defaults/Cargo.lock @@ -0,0 +1,5 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "structs" +version = "0.1.0" diff --git a/listings/ch05-using-structs-to-structure-related-data/no-listing-08-applying-defaults/Cargo.toml b/listings/ch05-using-structs-to-structure-related-data/no-listing-08-applying-defaults/Cargo.toml new file mode 100644 index 0000000000..7251aaa8de --- /dev/null +++ b/listings/ch05-using-structs-to-structure-related-data/no-listing-08-applying-defaults/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "structs" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/listings/ch05-using-structs-to-structure-related-data/no-listing-08-applying-defaults/src/main.rs b/listings/ch05-using-structs-to-structure-related-data/no-listing-08-applying-defaults/src/main.rs new file mode 100644 index 0000000000..1dad156e9e --- /dev/null +++ b/listings/ch05-using-structs-to-structure-related-data/no-listing-08-applying-defaults/src/main.rs @@ -0,0 +1,23 @@ +struct User { + active: bool, + username: String, + email: String, + sign_in_count: u64, +} + +// ANCHOR: here +fn build_user(email: String, username: String) -> User { + User { + username, + email, + .. + } +} +// ANCHOR_END: here + +fn main() { + let user1 = build_user( + String::from("someone@example.com"), + String::from("someusername123"), + ); +} diff --git a/src/ch05-01-defining-structs.md b/src/ch05-01-defining-structs.md index 66da938956..d794b8fe1d 100644 --- a/src/ch05-01-defining-structs.md +++ b/src/ch05-01-defining-structs.md @@ -100,6 +100,37 @@ named `email`. We want to set the `email` field’s value to the value in the the `email` parameter have the same name, we only need to write `email` rather than `email: email`. +### Defining default values for fields + +Some structs will have fields where there is a reasonable default value that +doesn't necessarily need to be specified every time the struct is created. Rust +lets you define _default field values_ for these cases. For the `User` struct, +this would make sense to apply to the `active` and `sign_in_count` field like +so: + ++ +```rust +{{#rustdoc_include ../listings/ch05-using-structs-to-structure-related-data/no-listing-07-default-field-value/src/main.rs:here}} +``` + + + +To apply the defaults, we use the `..` syntax. It specifies that the remaining +fields not explicitly set should use the default value from the struct definition. + ++ +```rust +{{#rustdoc_include ../listings/ch05-using-structs-to-structure-related-data/no-listing-08-applying-defaults/src/main.rs:here}} +``` + + + +Fields that don't have a default value still have to be explicitly set. When not +using the `..` syntax, the defaults are not used, and every field has to be set +explicitly. + @@ -123,8 +154,9 @@ otherwise use the same values from `user1` that we created in Listing 5-2. Using struct update syntax, we can achieve the same effect with less code, as -shown in Listing 5-7. The syntax `..` specifies that the remaining fields not -explicitly set should have the same value as the fields in the given instance. +shown in Listing 5-7. When followed by a struct instance, the `..` syntax +specifies that the remaining fields should have the same value as the fields in +the given instance.