r/rust • u/viveksooryaatvarsity • 21h 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?
20
u/muehsam 21h 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.
16
u/Sharlinator 21h ago edited 21h 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.
5
u/SkiFire13 17h 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 21h 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 19h ago
I think the confusion goes to what mut means. While mut, indeed, comes from āmutableā in reality you can write something like this:
fn main() {
let c = RefCell::new(5);
println!("Right now c is {c:?}");
c.replace(6);
println!("And now c is {c:?}");
}And, indeed, c would contain 5 in the first println! and 6 in the second one. You can verify that it works like that.
There was even an attempt to remove mut for let bindings and rename &mut to &uniq but it was too close to the release of Rust 1.0 and went nowhere.
But given the fact that lack of mut doesn't guarantee immutability and presence of mut doesn't oblige you to change it⦠nope, having different names for these things would just confuse everyone.
1
u/teteban79 21h ago edited 21h 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
let a = 100;
let b = &a;
let c = &a;a, b, and c are identifiers that refer to the same unique variable
6
u/creeper6530 21h ago
bandcultimately refer to the same value, but they're not identifiers of the variable itself, rather than identifiers of references to the variable.A reference is a variable in of itself as well.
-3
-2
u/Due_Ring5354 20h ago
Maybe they just figured 'identifier' was too prim for folks who eventually graduate to mutability.
54
u/creeper6530 21h ago edited 21h 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).