Skip to content

Commit f599516

Browse files
committed
Add speaker note with manual derive example
1 parent 347de61 commit f599516

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

src/methods-and-traits/deriving.md

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,28 @@ fn main() {
2626

2727
<details>
2828

29-
Derivation is implemented with macros, and many crates provide useful derive
30-
macros to add useful functionality. For example, `serde` can derive
31-
serialization support for a struct using `#[derive(Serialize)]`.
29+
- Derivation is implemented with macros, and many crates provide useful derive
30+
macros to add useful functionality. For example, `serde` can derive
31+
serialization support for a struct using `#[derive(Serialize)]`.
32+
33+
- Derivation is usually provided for traits that have a common boilerplate-y
34+
implementation that is correct for most cases. For example, demonstrate how a
35+
manual `Clone` impl can be repetitive compared to deriving the trait:
36+
37+
```rust,skip
38+
impl Clone for Player {
39+
fn clone(&self) -> Self {
40+
Player {
41+
name: self.name.clone(),
42+
strength: self.strength.clone(),
43+
hit_points: self.hit_points.clone(),
44+
}
45+
}
46+
}
47+
```
48+
49+
Not all of the `.clone()`s in the above are necessary in this case, but this
50+
demonstrates the generaly boilerplate-y pattern that manual impls would
51+
follow, which should help make the use of `derive` clear to students.
3252

3353
</details>

0 commit comments

Comments
 (0)