summaryrefslogtreecommitdiff
path: root/Rust struct init shorthand.md
blob: 01c740cff1e42d2f7380692f245417e506c5dfc8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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,
  }
}
```