Merge pull request #30 from rust-lang/section_three

More work on section 3
This commit is contained in:
Steve Klabnik
2015-12-17 11:56:53 -05:00
+136 -4
View File
@@ -151,13 +151,61 @@ 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.
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:
@@ -342,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.
@@ -352,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.
@@ -483,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
```