chapter ten

10 Anti-patterns

 

This chapter covers

  • Discussing programming anti-patterns
  • Reviewing common anti-patterns in Rust
  • Recognizing when to use and when to avoid contentious patterns

Anti-patterns are programming practices considered harmful in specific contexts or all circumstances. Anti-patterns are often the result of a misunderstanding of languages or a lack of experience with a particular technology stack. In this chapter, we’ll discuss some common anti-patterns in Rust and how to avoid them.

First, we must discuss what constitutes an anti-pattern and then explore Rust’s most common examples. We’ll also discuss how to recognize when to use–and when to avoid–specific patterns and when to make exceptions.

None of the rules presented in this chapter are hard and fast, and there are always exceptions. However, it’s important to understand these rules' reasoning and know when to break them. As Rust evolves, these rules may change, so staying up to date with the latest best practices is essential to writing Rust effectively.

10.1 What’s an anti-pattern?

10.2 unsafe

10.2.1 What does unsafe do?

10.2.2 Where can you use unsafe?

10.2.3 When should you use unsafe?

10.2.4 Should we worry about unsafe?

10.3 unwrap()

10.4 Not using Vec

10.5 Too many clones

10.6 Using Deref to emulate polymorphism

10.7 Global data and singletons

10.8 Too many smart pointers

10.9 Where to go from here

10.10 Summary