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: + +