rebuild pages at b996d8e

This commit is contained in:
Steve Klabnik
2015-12-17 16:59:24 +00:00
parent 203ff0d519
commit a6dc2dda2a
2 changed files with 210 additions and 8 deletions
+105 -4
View File
@@ -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!(&quot;x is not yet in scope&quot;);
let x = 5;
println!(&quot;x is now in scope&quot;);
println!(&quot;In real code, we’d now do a bunch of work.&quot;);
println!(&quot;x will go out of scope now! The next curly brace is ending the main function.&quot;);
}
</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!(&quot;x is not yet in scope&quot;);
let x = 5;
println!(&quot;x is now in scope&quot;);
println!(&quot;Let’s start a new scope!&quot;);
{
let y = 5;
println!(&quot;y is now in scope&quot;);
println!(&quot;x is also still in scope&quot;);
println!(&quot;y will go out of scope now!&quot;);
println!(&quot;The next curly brace is ending the scope we started.&quot;);
}
println!(&quot;x is still in scope, but y is now out of scope and is not usable&quot;);
println!(&quot;x will go out of scope now! The next curly brace is ending the main function.&quot;);
}
</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!(&quot;Before shadowing, x is: {}&quot;, x);
{
let x = 6;
println!(&quot;Now that x is shadowed, x is: {}&quot;, x);
}
println!(&quot;After shadowing, x is: {}&quot;, 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>