diff options
Diffstat (limited to 'Rust struct init shorthand.md')
-rw-r--r-- | Rust struct init shorthand.md | 16 |
1 files changed, 16 insertions, 0 deletions
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, + } +} +``` + |