What is immutability?
When we talk about immutability in this chapter, we are talking about structs that have a state that does not change over time. In other words, when a struct is created, that is how that specific struct will be represented during its lifetime. We can still create new structs and delete old ones. So, the state at the system level will effectively change by new structs being created and old structs being deleted. This has several advantages:
- First, because our structs are not changing, we can safely pass data to a function and know that, whatever happens, the copy that we passed to the function will remain intact.
- Secondly, immutable structs make it easier to write correct, concurrent code. As the state of the struct cannot be changed by any function calling it, we can safely parallelize execution and call multiple functions using the same struct as input data.
- And third, this makes our code easier to reason about. At each step of the way, the state...