Concept

Immutability And Persistent Data Structures

Immutability keeps values from changing after creation, while persistent data structures preserve previous versions through structural sharing instead of destructive updates.

What Immutability Means

Immutable values do not change after they are created. Instead of updating a value in place, code creates a new value that represents the next state. Persistent data structures make this practical by sharing unchanged structure between old and new versions.

Clojure makes persistent immutable collections central. Scala and F# provide immutable collection families. Rust makes bindings immutable by default but still distinguishes immutable binding from immutable data behind references, cells, locks, or other abstractions.

Why It Matters

Immutability reduces accidental shared mutation. That helps with concurrency, undo/redo, snapshots, pure functions, caching, state transitions, and testing. If old versions remain valid, readers can hold references without fearing that another part of the program changed the value underneath them.

The cost is not always zero. Persistent structures can allocate, retain memory through shared history, and have different performance profiles than mutable arrays, hash tables, or buffers. Some domains still need in-place mutation for hardware, tight loops, streaming I/O, or large numeric arrays.

Watch Points

Do not treat "immutable" as a universal performance or correctness guarantee. Check whether nested data is also immutable, whether references expose mutation, whether builders or transient mutable phases exist, and whether old versions are retained longer than expected.

Related Concepts

This concept pairs with Functional Programming, Threads And Shared Memory, Data Races And Memory Models, and Garbage Collection.

Sources

Last verified:

  1. Clojure Reference - Data Structures Clojure
  2. Scala Collections Overview Scala
  3. The Rust Programming Language - Variables and Mutability Rust Project
  4. F# Collections Microsoft Learn