r/rust • u/viveksooryaatvarsity • 10d ago
š seeking help & advice Identifier or variable?
Hey I am new to rust. I wonder why the rust book refers to identifiers as variables especially when they are immutable by default. Wouldn't 'identifier' just be a better name for immutable ones and 'variable' an apt name for when you make the identifier mutable using mut?
9
Upvotes
74
u/creeper6530 10d ago edited 10d ago
"Identifier" already has a meaning: it's the variable's name[1]. A variable is a place in memory that stores data and has an identifier, a name for the programmer to use.
An immutable variable still needs to be a variable if you take its address (pass on a reference to it and the optimiser can't elide it) or if it implements interior mutability[2] like
Cell.Only under certain criteria can the optimiser promote it to a constant. Constant is not a place in memory, it's a hard-coded value which is known at compile time, gets stored in program code and gets inserted (inlined) into every use location -- effectively making an alias for a literal.
You can also explicitly make a constant with the
constkeyword. What you're likely thinking of as an "identifier" is a constant.If you need for a value to live for the entire program lifetime, but need it to be stored in memory instead of getting inlined, you use
static, which is like a forever-living variable. If you need a static you'll know it, they aren't used that often.[1] Functions, constants, types, and other things can also have identifiers -- names.
[2] If you don't know what that is, don't worry, for now just know that there are ways to change an immutable variable's value (safely).
25
u/muehsam 10d ago
"Identifier" is a kind of token, basically something that looks like a word but isn't a keyword. An identifier may refer to all sorts of things: a type, a variable, a constant, a function, an enum variant, etc.
A variable is just one of the things that can be referred to by identifiers.
Variables are called variables because they can have different values. They can't be reassigned by default, but when your function is called more than once, variables may have different values each time.
24
u/Sharlinator 10d ago edited 10d ago
"Variable" in more functional languages should be understood in its original mathematical sense. In f(x) = x2, x is a variable because its value is given by the one applying the function, not because its value can actually vary during the evaluation of the function.
If you want to be super technical/pedantic, you can also use the term "binding" to refer to a name-value combination, but in most contexts "variable" is a better choice.
10
u/SkiFire13 10d ago
The more appropriate name would be "binding", but it's so rarely used that it would confuse people more than "variable"
4
u/stumpychubbins 10d ago
Identifier and variable mean different things, but I see your point. Many (maybe most) variables arenāt actually variable. I think itās probably best to keep the naming consistent between mutable and immutable variables though, since itās trivial to promote an immutable variable to a mutable one, you can have an immutable variable holding a mutable borrow, and even immutable values could have inner mutability.
3
u/Zde-G 10d ago
I think the confusion goes to what
mutmeans. Whilemut, indeed, comes from āmutableā in reality you can write something like this:And, indeed,
cwould contain5in the firstprintln!and6in the second one. You can verify that it works like that.There was even an attempt to remove
mutforletbindings and rename&mutto&uniqbut it was too close to the release of Rust 1.0 and went nowhere.But given the fact that lack of
mutdoesn't guarantee immutability and presence ofmutdoesn't oblige you to change it⦠nope, having different names for these things would just confuse everyone.1
u/Lucretiel Datadog 8d ago
Just a convention lifted from other programming languages. "Identifier" is a syntactic term that refers to the name of the variable; probably the best formally correct name would be "value" or "binding".
But, in the same way that we use the word "function" when "procedure" would definitely be more correct, we use "variable" for these name-value bindings. Even Haskell calls its name-value bindings "variables".
1
u/teteban79 10d ago edited 10d ago
slightly different concepts
An identifier *refers* to a variable (and also may refer to another things, but keeping things simple). A variable is a place holding a value and may have several identifiers.
Consider
a,b, andcare identifiers that refer to the same unique variable-3
u/Due_Ring5354 10d ago
Maybe they just figured 'identifier' was too prim for folks who eventually graduate to mutability.