stevedonovan/gentle-intro

1-basics: variability vs mutability

eddyb opened this issue · 6 comments

eddyb commented

I already tried to summarize it at rust-lang/book#276 (comment) for the second-edition book.
But the very short version of it is:

  • "variable" => "may not have the same value every time" (e.g. user input)
  • "mutable" => "the value may further change during the scope of the variable"

And the reason "immutable variable" is confusing is because all variables in some languages are mutable by default (i.e. without const or final), not because "variable" implies mutability.

(You may already know all of this, but I wanted to make sure we're not promoting "it is so because we say so" explanations, when the terms we use are consistent with other disciplines, e.g. mathematics)

Good point. I'm trying to speak to the usual sense of 'variable' in programming, but it's more the mathematical sense here. And 'variable' vs 'constant' is the important thing.

eddyb commented

@stevedonovan They're the same meaning! It's just that the defaults differ, like with references.

I've fleshed it out a little. Yes, variables are variables. But the mutable default that is so common confuses people.

Should this be closed with the changes you made? They seems good enough.

"immutable variable" is an oxymoronic contradiction in normal english.

eddyb commented

@pannous In some sense "variable" is short for "variable binding" - the value inside is the variable, and what you're mutating is the "variable binding", not the value (which varies for a different reason than mutation - e.g. function arguments can differ between calls).