Skip to content

Suggestion for chapter about bindings #28177

Closed
@gor77

Description

@gor77

Hi,

I feel variable bindings need to be explained more.

Working and learning alone, it took me quite some time to figure them out
(hopefuly I got everything right). Things like:

  1. Keyword let declares a new binding - sort of linkage between name and value.
    It has to be used ONLY when you want to introduce a new binding:
fn main() {
    x = 2; // this is an error
}
  1. You can have two or more bindings with the same name. The one declared
    last will "win" (or shadow) over the previous one
fn main() {
    let x = 2;
    let x = 3; //no error here, from now on x is 3
}
  1. Each new binding can have its mutability independently declared,
    even if it has the same name as one before
fn main() {

    let x = 2;
    let mut x = 3; // from now on, x has value 3 and it can be changed

}

These things are simply not stressed enough in the book (or not mentioned at all) and understanding these things is the basis for understanding something like this:

let x = 2; // this is immutable binding and from now on we can't modify x
let mut x = x; // but when we create a new mutable binding with the same name
x = 3; //...then we can change x

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions