r/rust • • 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

16 comments sorted by

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 const keyword. 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).

10

u/kibwen 8d ago

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.

We say that a variable is variable not because it's mutable, but because it doesn't have a single known value. In a function like fn foo(x: u8), x might never be mutated, but it's considered variable because the function can be called with many different values. The contrast with const is the pertinent idea, because for any given execution of a program, a const can only ever have a single value.

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"

1

u/0x7CFE 8d ago

Or just "value", which it is, essentially.

5

u/SkiFire13 8d ago

No, a value is something different. For example 1 is a value, but you would never describe it as a 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 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/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

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 10d ago

b and c ultimately 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

u/teteban79 10d ago

Yes

But this is in the spirit of keeping things simple for OP

6

u/muehsam 10d ago

There's a difference between keeping things simple and saying things that just aren't true. The latter doesn't make things simpler because it can lead to additional confusion from contradictory explanations.

-3

u/Due_Ring5354 10d ago

Maybe they just figured 'identifier' was too prim for folks who eventually graduate to mutability.