mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-04-01 17:34:25 +02:00
Add speaker note with manual derive example (#2607)
This commit is contained in:
parent
bb8cbbcb7c
commit
12fb4379f7
@ -26,8 +26,28 @@ fn main() {
|
|||||||
|
|
||||||
<details>
|
<details>
|
||||||
|
|
||||||
Derivation is implemented with macros, and many crates provide useful derive
|
- Derivation is implemented with macros, and many crates provide useful derive
|
||||||
macros to add useful functionality. For example, `serde` can derive
|
macros to add useful functionality. For example, `serde` can derive
|
||||||
serialization support for a struct using `#[derive(Serialize)]`.
|
serialization support for a struct using `#[derive(Serialize)]`.
|
||||||
|
|
||||||
|
- Derivation is usually provided for traits that have a common boilerplate-y
|
||||||
|
implementation that is correct for most cases. For example, demonstrate how a
|
||||||
|
manual `Clone` impl can be repetitive compared to deriving the trait:
|
||||||
|
|
||||||
|
```rust,ignore
|
||||||
|
impl Clone for Player {
|
||||||
|
fn clone(&self) -> Self {
|
||||||
|
Player {
|
||||||
|
name: self.name.clone(),
|
||||||
|
strength: self.strength.clone(),
|
||||||
|
hit_points: self.hit_points.clone(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Not all of the `.clone()`s in the above are necessary in this case, but this
|
||||||
|
demonstrates the generally boilerplate-y pattern that manual impls would
|
||||||
|
follow, which should help make the use of `derive` clear to students.
|
||||||
|
|
||||||
</details>
|
</details>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user