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 the idea that after a value has been declared and assigned, it cannot be modified (or mutated). Contrast this concept with mutability, in which 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 most underloved ...

Get Idiomatic Rust now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.