9 Immutability
This chapter covers
- Understanding the benefits of immutability
- Thinking in terms of immutable data and how it works in Rust
- Using traits to make nearly anything immutable
- Exploring crates that provide immutable data structures
Immutability is a powerful concept that can help everyone build better software. Immutability as it relates to writing software is simply the idea that once a value has been declared and assigned, it cannot be modified (or mutated). This is in contrast to mutability, where a value can be altered after it has been declared. In other words, values that can be changed are mutable, and values that are never changed are immutable.
Immutability is an important design pattern and one of the more underloved and underappreciated ones at that. I feel it’s so valuable that I’m dedicating an entire chapter of this book to the subject, although it deserves an entire book. I won’t be able to go into as much depth in this book as I’d like, but I will leave you with a great starting point to explore the topic further.