Skip to content

Commit 8dd749d

Browse files
authored
Update field-shorthand.md (#227)
Introducing `Self` type that students can encounter in many places and nice to use. It is only mentioned later in the course, but it is not defined currently.
1 parent 356d1ce commit 8dd749d

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/structs/field-shorthand.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,17 @@ fn main() {
2121
println!("{peter:?}");
2222
}
2323
```
24+
25+
<details>
26+
27+
The `new` function could be written using `Self` as a type, as it is interchangeable with the struct type name
28+
29+
```rust,ignore
30+
impl Person {
31+
fn new(name: String, age: u8) -> Self {
32+
Self { name, age }
33+
}
34+
}
35+
```
36+
37+
</details>

0 commit comments

Comments
 (0)