mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-04-01 17:34:25 +02:00
Move struct update syntax to speaker notes (#2597)
This commit is contained in:
parent
ca137b4807
commit
e1ed6eaf47
@ -27,9 +27,6 @@ fn main() {
|
|||||||
let age = 39;
|
let age = 39;
|
||||||
let avery = Person { name, age };
|
let avery = Person { name, age };
|
||||||
describe(&avery);
|
describe(&avery);
|
||||||
|
|
||||||
let jackie = Person { name: String::from("Jackie"), ..avery };
|
|
||||||
describe(&jackie);
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -49,8 +46,20 @@ Key Points:
|
|||||||
not important.
|
not important.
|
||||||
- If you already have variables with the right names, then you can create the
|
- If you already have variables with the right names, then you can create the
|
||||||
struct using a shorthand.
|
struct using a shorthand.
|
||||||
- The syntax `..avery` allows us to copy the majority of the fields from the old
|
|
||||||
struct without having to explicitly type it all out. It must always be the
|
## More to Explore
|
||||||
last element.
|
|
||||||
|
- You can also demonstrate the struct update syntax here:
|
||||||
|
|
||||||
|
```rust,ignore
|
||||||
|
let jackie = Person { name: String::from("Jackie"), ..avery };
|
||||||
|
```
|
||||||
|
|
||||||
|
- It allows us to copy the majority of the fields from the old struct without
|
||||||
|
having to explicitly type it all out. It must always be the last element.
|
||||||
|
|
||||||
|
- It is mainly used in combination with the `Default` trait. We will talk about
|
||||||
|
struct update syntax in more detail on the slide on the `Default` trait, so we
|
||||||
|
don't need to talk about it here unless students ask about it.
|
||||||
|
|
||||||
</details>
|
</details>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user