From 977219655e5fee797536fe90eff08007266d1d5d Mon Sep 17 00:00:00 2001 From: Steve Klabnik Date: Thu, 17 Dec 2015 11:30:07 -0500 Subject: [PATCH 1/3] Add section about type annotations --- src/variable-bindings.md | 46 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/src/variable-bindings.md b/src/variable-bindings.md index 9bb56357e..e945600bc 100644 --- a/src/variable-bindings.md +++ b/src/variable-bindings.md @@ -158,6 +158,52 @@ let y = 6; In simple cases like this, two `let`s may be clearer, but in others, creating multiple bindings at once is nice. As we become more proficient in Rust, we’ll figure out which style is better, but it’s mostly a judgement call. +## Type annotations + +You may have noticed that we didn’t declare the type of `x` or `y` in our previous examples. +Rust is a *statically typed* language, which means that at compile time, we must know the types of all bindings. +But annotating every single binding with a type can feel like busywork, and make code noisy. +To solve this issue, Rust uses ‘type inference’, meaning that it attempts to infer the types of your bindings. + +The primary way that the type is inferred is by looking at how it is used. +Let’s look at the example again: + +```rust +fn main() { + let x = 5; +} +``` + +When we bind `x` to `5`, the compiler knows that `x` should be a numeric type. +Without any other information, it defaults to `i32`, a thirty-two bit integer type. +We’ll talk more about Rust’s basic types in section 3.3. + +Here’s what a `let` statement with a ‘type annotation’ looks like: + +```rust +fn main() { + let x: i32 = 5; +} +``` + +We can add a colon, followed by the type name. +Here’s the structure of a `let` statement with a type annotation: + +```text +let PATTERN: TYPE = VALUE; +``` + +Note that the colon and the `TYPE` go _after_ the `PATTERN`, not in the pattern itself. +As an example, here’s our more complex pattern with two bindings: + +```rust +fn main() { + let (x, y): (i32, i32) = (5, 6); +} +``` + +Just like we match up the `VALUE` with the `PATTERN`, we match up the `TYPE` with the `PATTERN`. + ## Initialization We do not have to provide bindings with an initial value, and can assign it later. Try this program: From 4879838a040fa9a61f8297741d00268cb5eececf Mon Sep 17 00:00:00 2001 From: Steve Klabnik Date: Thu, 17 Dec 2015 11:31:49 -0500 Subject: [PATCH 2/3] Always use fn main() We should use main in this Basics section, and stop afterwards. --- src/variable-bindings.md | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/variable-bindings.md b/src/variable-bindings.md index e945600bc..25d6f54f5 100644 --- a/src/variable-bindings.md +++ b/src/variable-bindings.md @@ -151,8 +151,10 @@ As you can see, the two line up visually, and so `let` binds `5` to `x` and `6` We could have used two `let` statements as well: ```rust -let x = 5; -let y = 6; +fn main() { + let x = 5; + let y = 6; +} ``` In simple cases like this, two `let`s may be clearer, but in others, creating multiple bindings at once is nice. @@ -388,8 +390,11 @@ There is one subtlety we haven’t covered yet: `mut` allows you to mutate _the In other words: ```rust -let mut x = 5; -x = 6; +fn main() { + let mut x = 5; + + x = 6; +} ``` This is not changing the value that `x` is bound to, but creating a new value, `6`, and changing the binding to bind to it instead. From da0c5f6ef30233ab585e41c44b723d76f21eca6a Mon Sep 17 00:00:00 2001 From: Steve Klabnik Date: Thu, 17 Dec 2015 11:43:27 -0500 Subject: [PATCH 3/3] Add more information about scope --- src/variable-bindings.md | 81 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/src/variable-bindings.md b/src/variable-bindings.md index 25d6f54f5..e68bd4af0 100644 --- a/src/variable-bindings.md +++ b/src/variable-bindings.md @@ -403,6 +403,55 @@ Well, for now, it does not make a lot of difference, but when our programs get m Specifically, passing arguments to functions will illustrate the difference. We’ll talk about that in the next section, when we discuss functions. +## Scope + +Variable bindings have a ‘scope’ in which they’re valid. +That scope begins from the point at which the binding is declared, and ends at the end of the next block of code. +We can only access bindings which are ‘in scope’. +We cannot access them ‘before they come into scope’ or ‘after they go out of scope’. +Here’s an example: + +```rust +fn main() { + println!("x is not yet in scope"); + + let x = 5; + println!("x is now in scope"); + + println!("In real code, we’d now do a bunch of work."); + + println!("x will go out of scope now! The next curly brace is ending the main function."); +} +``` + +We can create arbitrary scopes through the use of `{` and `}`: + +```rust +fn main() { + println!("x is not yet in scope"); + + let x = 5; + println!("x is now in scope"); + + println!("Let’s start a new scope!"); + + { + let y = 5; + println!("y is now in scope"); + println!("x is also still in scope"); + + println!("y will go out of scope now!"); + println!("The next curly brace is ending the scope we started."); + } + + println!("x is still in scope, but y is now out of scope and is not usable"); + + println!("x will go out of scope now! The next curly brace is ending the main function."); +} +``` + +What bindings are in and out of scope will become much more important later, once we learn about ‘references’ and ‘traits’. + ## Shadowing A final thing about bindings: they can ‘shadow’ previous bindings with the same name. @@ -534,3 +583,35 @@ Our program would work just fine without it. It’s worth listening to these warnings, and fixing the problems they point out. They can be signs of a larger problem. In this case, we may not have realized that we were shadowing `x`. + +### Shadowing and scopes + +Like any binding, a binding that shadows another binding will go away at the end of a scope. +Here’s an example program: + +```rust +fn main() { + let x = 5; + + println!("Before shadowing, x is: {}", x); + + { + let x = 6; + + println!("Now that x is shadowed, x is: {}", x); + } + + println!("After shadowing, x is: {}", x); +} +``` + +If we run this example, we can see the shadow appear and disappear: + +```bash +$ cargo run + Compiling bindings v0.1.0 (file:///home/steve/tmp/bindings) + Running `target/debug/bindings` +Before shadowing, x is: 5 +Now that x is shadowed, x is: 6 +After shadowing, x is: 5 +```