@@ -4,6 +4,25 @@ We've covered quite a few different traits in this chapter—and we've only scra
4
4
It may feel like you have a lot to remember, but don't worry: you'll bump into these traits
5
5
so often when writing Rust code that they'll soon become second nature.
6
6
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
+
7
26
Before moving on, let's go through one last exercise to consolidate what we've learned.
8
27
You'll have minimal guidance this time—just the exercise description and the tests to guide you.
9
28
0 commit comments