Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion for chapter about bindings #28177

Closed
gor77 opened this issue Sep 2, 2015 · 1 comment · Fixed by #28743
Closed

Suggestion for chapter about bindings #28177

gor77 opened this issue Sep 2, 2015 · 1 comment · Fixed by #28743

Comments

@gor77
Copy link

gor77 commented Sep 2, 2015

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
@xiaochuanyu
Copy link
Contributor

Just a note. Comparing to Rust By Example section about bindings, shadowing and declaring uninitialized bindings have been left out.

@sfackler sfackler added the A-docs label Sep 3, 2015
JanLikar added a commit to JanLikar/rust that referenced this issue Sep 30, 2015
 - Expand the first paragraph

 - Improve readability by partitioning the chapter into the following
   sections: "Patterns", "Type annotations", "Mutability", and
   "Initializing bindings"

 - Add "Scope and shadowing" section (fix rust-lang#28177)
Manishearth added a commit to Manishearth/rust that referenced this issue Sep 30, 2015
 - Expand the first paragraph

 - Improve readability by partitioning the chapter into the following
   sections: "Patterns", "Type annotations", "Mutability", and
   "Initializing bindings"

 - Add "Scope and shadowing" section (fix rust-lang#28177)

r? @steveklabnik
steveklabnik added a commit to steveklabnik/rust that referenced this issue Sep 30, 2015
 - Expand the first paragraph

 - Improve readability by partitioning the chapter into the following
   sections: "Patterns", "Type annotations", "Mutability", and
   "Initializing bindings"

 - Add "Scope and shadowing" section (fix rust-lang#28177)

r? @steveklabnik
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants