1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-01-09 09:11:50 +02:00

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.
This commit is contained in:
Igor Petruk 2023-01-23 11:32:46 +00:00 committed by GitHub
parent 356d1cec19
commit 8dd749da60
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -21,3 +21,17 @@ fn main() {
println!("{peter:?}");
}
```
<details>
The `new` function could be written using `Self` as a type, as it is interchangeable with the struct type name
```rust,ignore
impl Person {
fn new(name: String, age: u8) -> Self {
Self { name, age }
}
}
```
</details>