mirror of
https://github.com/rust-lang/book.git
synced 2026-11-30 20:04:31 -06:00
rebuild pages at b996d8e
This commit is contained in:
+105
-4
@@ -165,11 +165,43 @@ Here’s our pattern:</p>
|
||||
</code></pre>
|
||||
<p>As you can see, the two line up visually, and so <code>let</code> binds <code>5</code> to <code>x</code> and <code>6</code> to <code>y</code>.
|
||||
We could have used two <code>let</code> statements as well:</p>
|
||||
<pre><code class="language-rust">let x = 5;
|
||||
let y = 6;
|
||||
<pre><code class="language-rust">fn main() {
|
||||
let x = 5;
|
||||
let y = 6;
|
||||
}
|
||||
</code></pre>
|
||||
<p>In simple cases like this, two <code>let</code>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.</p>
|
||||
<h2>Type annotations</h2>
|
||||
<p>You may have noticed that we didn’t declare the type of <code>x</code> or <code>y</code> in our previous examples.
|
||||
Rust is a <em>statically typed</em> 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.</p>
|
||||
<p>The primary way that the type is inferred is by looking at how it is used.
|
||||
Let’s look at the example again:</p>
|
||||
<pre><code class="language-rust">fn main() {
|
||||
let x = 5;
|
||||
}
|
||||
</code></pre>
|
||||
<p>When we bind <code>x</code> to <code>5</code>, the compiler knows that <code>x</code> should be a numeric type.
|
||||
Without any other information, it defaults to <code>i32</code>, a thirty-two bit integer type.
|
||||
We’ll talk more about Rust’s basic types in section 3.3.</p>
|
||||
<p>Here’s what a <code>let</code> statement with a ‘type annotation’ looks like:</p>
|
||||
<pre><code class="language-rust">fn main() {
|
||||
let x: i32 = 5;
|
||||
}
|
||||
</code></pre>
|
||||
<p>We can add a colon, followed by the type name.
|
||||
Here’s the structure of a <code>let</code> statement with a type annotation:</p>
|
||||
<pre><code class="language-text">let PATTERN: TYPE = VALUE;
|
||||
</code></pre>
|
||||
<p>Note that the colon and the <code>TYPE</code> go <em>after</em> the <code>PATTERN</code>, not in the pattern itself.
|
||||
As an example, here’s our more complex pattern with two bindings:</p>
|
||||
<pre><code class="language-rust">fn main() {
|
||||
let (x, y): (i32, i32) = (5, 6);
|
||||
}
|
||||
</code></pre>
|
||||
<p>Just like we match up the <code>VALUE</code> with the <code>PATTERN</code>, we match up the <code>TYPE</code> with the <code>PATTERN</code>.</p>
|
||||
<h2>Initialization</h2>
|
||||
<p>We do not have to provide bindings with an initial value, and can assign it later. Try this program:</p>
|
||||
<pre><code class="language-rust">fn main() {
|
||||
@@ -308,14 +340,58 @@ The <code>mut</code> only applies to the name that follows it, not the whole pat
|
||||
<h3>Reassignment, not mutation</h3>
|
||||
<p>There is one subtlety we haven’t covered yet: <code>mut</code> allows you to mutate <em>the binding</em>, but not <em>what the binding binds to</em>.
|
||||
In other words:</p>
|
||||
<pre><code class="language-rust">let mut x = 5;
|
||||
x = 6;
|
||||
<pre><code class="language-rust">fn main() {
|
||||
let mut x = 5;
|
||||
|
||||
x = 6;
|
||||
}
|
||||
</code></pre>
|
||||
<p>This is not changing the value that <code>x</code> is bound to, but creating a new value, <code>6</code>, and changing the binding to bind to it instead.
|
||||
It’s a subtle but important difference.
|
||||
Well, for now, it does not make a lot of difference, but when our programs get more complex, it will.
|
||||
Specifically, passing arguments to functions will illustrate the difference.
|
||||
We’ll talk about that in the next section, when we discuss functions.</p>
|
||||
<h2>Scope</h2>
|
||||
<p>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:</p>
|
||||
<pre><code class="language-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.");
|
||||
}
|
||||
</code></pre>
|
||||
<p>We can create arbitrary scopes through the use of <code>{</code> and <code>}</code>:</p>
|
||||
<pre><code class="language-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.");
|
||||
}
|
||||
</code></pre>
|
||||
<p>What bindings are in and out of scope will become much more important later, once we learn about ‘references’ and ‘traits’.</p>
|
||||
<h2>Shadowing</h2>
|
||||
<p>A final thing about bindings: they can ‘shadow’ previous bindings with the same name.
|
||||
Here’s a sample program:</p>
|
||||
@@ -417,6 +493,31 @@ 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 <code>x</code>.</p>
|
||||
<h3>Shadowing and scopes</h3>
|
||||
<p>Like any binding, a binding that shadows another binding will go away at the end of a scope.
|
||||
Here’s an example program:</p>
|
||||
<pre><code class="language-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);
|
||||
}
|
||||
</code></pre>
|
||||
<p>If we run this example, we can see the shadow appear and disappear:</p>
|
||||
<pre><code class="language-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
|
||||
</code></pre>
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user