Skip to content

Commit e8cc121

Browse files
Add trait recommendations.
1 parent 1993f43 commit e8cc121

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

book/src/04_traits/14_outro.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,25 @@ We've covered quite a few different traits in this chapter—and we've only scra
44
It may feel like you have a lot to remember, but don't worry: you'll bump into these traits
55
so often when writing Rust code that they'll soon become second nature.
66

7+
## Closing thoughts
8+
9+
Traits are powerful, but don't overuse them.
10+
A few guidelines to keep in mind:
11+
12+
- Don't make a function generic if it is always invoked with a single type. It introduces indirection in your
13+
codebase, making it harder to understand and maintain.
14+
- Don't create a trait if you only have one implementation. It's a sign that the trait is not needed.
15+
- Implement standard traits for your types (`Debug`, `PartialEq`, etc.) whenever it makes sense.
16+
It will make your types more idiomatic and easier to work with, unlocking a lot of functionality provided
17+
by the standard library and ecosystem crates.
18+
- Implement traits from third-party crates if you need the functionality they unlock within their ecosystem.
19+
- Beware of making code generic solely to use mocks in your tests. The maintainability cost of this approach
20+
can be high, and it's often better to use a different testing strategy. Check out the
21+
[testing masterclass](https://github.com/mainmatter/rust-advanced-testing-workshop)
22+
for details on high-fidelity testing.
23+
24+
## Testing your knowledge
25+
726
Before moving on, let's go through one last exercise to consolidate what we've learned.
827
You'll have minimal guidance this time—just the exercise description and the tests to guide you.
928

0 commit comments

Comments
 (0)