From 27514d58573ce1f844af4ea60afa72c7f58f1cd7 Mon Sep 17 00:00:00 2001 From: Jasper Ras Date: Thu, 21 Aug 2025 22:09:29 +0200 Subject: vault backup: 2025-08-21 22:09:29 --- Rust struct init shorthand.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 Rust struct init shorthand.md (limited to 'Rust struct init shorthand.md') diff --git a/Rust struct init shorthand.md b/Rust struct init shorthand.md new file mode 100644 index 0000000..01c740c --- /dev/null +++ b/Rust struct init shorthand.md @@ -0,0 +1,16 @@ +A struct can be initialized in a function like we do in Go. Rust has a nice shorthand: if the function args are named identically to the struct fields we don't have to pass the struct field name: + +```rust +struct User { + name: String, + age: i8, +} + +fn init_user(name: String, what: i8) -> User { + User { + name <- shorthand + age: what, + } +} +``` + -- cgit v1.2.3