Search code examples
rustnomenclature

Difference between variable bindings and variables?


Over my time using Rust, I’ve seen some people call them variables whilst others call them variable bindings - or even bindings as a short way of referring to it.

In the Rust book, it’s stated:

In many languages, a variable binding would be called a variable, but Rust’s variable bindings have a few tricks up their sleeves. For example the left-hand side of a let statement is a ‘pattern’, not a variable name.

Later in the book, they go on referring to variable bindings as either bindings or variables. The same I’ve noticed here on Stackoverflow - it seems as though they’e used interchangably.

The question is whether or not there a time when referring to bindings as variables is more correct than the latter? I would assume both are different words for the more correct term - variable bindings - should you follow the words from their book. I’m confused.


Solution

  • A variable binding is a statement that binds a variable to a name; in the statement

    let x = 1;
    

    The whole statement is the variable binding and x is the name the variable gets bound to. After this statement you may refer to x as a variable or a binding (but not a variable binding).

    The fragment you brought up just emphasizes the fact that in Rust you can do all kinds of stuff with variable bindings since let statements are subject to pattern matching, e.g.

    let (a, b, c, s) = (1u8, 2usize, 3i32, "foo");
    

    Assigns 4 variables of different types at once.

    Edit: there might be a reason why binding is such a popular term with Rust even though variable is a much more established term in programming; since in Rust bindings are immutable by default, it might be weird for people without a programming background to call them variables.