diff options
Diffstat (limited to 'Rust struct update syntax.md')
-rw-r--r-- | Rust struct update syntax.md | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/Rust struct update syntax.md b/Rust struct update syntax.md new file mode 100644 index 0000000..299ac55 --- /dev/null +++ b/Rust struct update syntax.md @@ -0,0 +1,22 @@ +Instantiate a new instance of a struct with most of the fields from an other instance: + +```rust +struct User { + name: String, + email: String, +} + +fn main () { + let user1 = User { + name: String::from("Jasper"), + email: String::from("jaspert.ras@gmail.com"), + } + + let user2 = User { + email: String::from("maffe_jasper@hotmail.nl"), + ..user1 + } +} +``` + +Will reuse `name` from `user1`.
\ No newline at end of file |