1
1
% Variable Bindings
2
2
3
3
Virtually every non-'Hello World’ Rust program uses * variable bindings* . They
4
- look like this:
4
+ bind some value to a name, so it can be used later. ` let ` is
5
+ used to introduce a binding, just like this:
5
6
6
7
``` rust
7
8
fn main () {
@@ -13,10 +14,12 @@ Putting `fn main() {` in each example is a bit tedious, so we’ll leave that ou
13
14
in the future. If you’re following along, make sure to edit your ` main() `
14
15
function, rather than leaving it off. Otherwise, you’ll get an error.
15
16
16
- In many languages, this is called a * variable* , but Rust’s variable bindings
17
- have a few tricks up their sleeves. For example the left-hand side of a ` let `
18
- expression is a ‘[ pattern] [ pattern ] ’, not just a variable name. This means we
19
- can do things like:
17
+ # Patterns
18
+
19
+ In many languages, a variable binding would be called a * variable* , but Rust’s
20
+ variable bindings have a few tricks up their sleeves. For example the
21
+ left-hand side of a ` let ` expression is a ‘[ pattern] [ pattern ] ’, not just a
22
+ variable name. This means we can do things like:
20
23
21
24
``` rust
22
25
let (x , y ) = (1 , 2 );
@@ -29,6 +32,8 @@ of our minds as we go forward.
29
32
30
33
[ pattern ] : patterns.html
31
34
35
+ # Type annotations
36
+
32
37
Rust is a statically typed language, which means that we specify our types up
33
38
front, and they’re checked at compile time. So why does our first example
34
39
compile? Well, Rust has this thing called ‘type inference’. If it can figure
@@ -63,6 +68,8 @@ Note the similarities between this annotation and the syntax you use with
63
68
occasionally include them to help you understand what the types that Rust
64
69
infers are.
65
70
71
+ # Mutability
72
+
66
73
By default, bindings are * immutable* . This code will not compile:
67
74
68
75
``` rust,ignore
@@ -97,9 +104,11 @@ out of the scope of this guide. In general, you can often avoid explicit
97
104
mutation, and so it is preferable in Rust. That said, sometimes, mutation is
98
105
what you need, so it’s not verboten.
99
106
100
- Let’s get back to bindings. Rust variable bindings have one more aspect that
101
- differs from other languages: bindings are required to be initialized with a
102
- value before you're allowed to use them.
107
+ # Initializing bindings
108
+
109
+ Rust variable bindings have one more aspect that differs from other languages:
110
+ bindings are required to be initialized with a value before you're allowed to
111
+ use them.
103
112
104
113
Let’s try it out. Change your ` src/main.rs ` file to look like this:
105
114
@@ -167,3 +176,77 @@ For now, we'll just stick to the default: integers aren't very complicated to
167
176
print.
168
177
169
178
[ format ] : ../std/fmt/index.html
179
+
180
+ # Scope and shadowing
181
+
182
+ Let’s get back to bindings. Variable bindings have a scope - they are
183
+ constrained to live in a block they were defined in. A block is a collection
184
+ of statements enclosed by ` { ` and ` } ` . Function definitions are also blocks!
185
+ In the following example we define two variable bindings, ` x ` and ` y ` , which
186
+ live in different blocks. ` x ` can be accessed from inside the ` fn main() {} `
187
+ block, while ` y ` can be accessed only from inside the inner block:
188
+
189
+ ``` rust,ignore
190
+ fn main() {
191
+ let x: i32 = 17;
192
+ {
193
+ let y: i32 = 3;
194
+ println!("The value of x is {} and value of y is {}", x, y);
195
+ }
196
+ println!("The value of x is {} and value of y is {}", x, y); // This won't work
197
+ }
198
+ ```
199
+
200
+ The first ` println! ` would print "The value of x is 17 and the value of y is
201
+ 3", but this example cannot be compiled successfully, because the second
202
+ ` println! ` cannot access the value of ` y ` , since it is not in scope anymore.
203
+ Instead we get this error:
204
+
205
+ ``` bash
206
+ $ cargo build
207
+ Compiling hello v0.1.0 (file:///home/you/projects/hello_world)
208
+ main.rs:7:62: 7:63 error: unresolved name ` y` . Did you mean ` x` ? [E0425]
209
+ main.rs:7 println! (" The value of x is {} and value of y is {}" , x, y); // This won' t work
210
+ ^
211
+ note: in expansion of format_args!
212
+ <std macros>:2:25: 2:56 note: expansion site
213
+ <std macros>:1:1: 2:62 note: in expansion of print!
214
+ <std macros>:3:1: 3:54 note: expansion site
215
+ <std macros>:1:1: 3:58 note: in expansion of println!
216
+ main.rs:7:5: 7:65 note: expansion site
217
+ main.rs:7:62: 7:63 help: run `rustc --explain E0425` to see a detailed explanation
218
+ error: aborting due to previous error
219
+ Could not compile `hello`.
220
+
221
+ To learn more, run the command again with --verbose.
222
+ ```
223
+
224
+ Additionaly, variable bindings can be shadowed. This means that a later
225
+ variable binding with the same name as another binding, that' s currently in
226
+ scope, will override the previous binding.
227
+
228
+ ` ` ` rust
229
+ let x: i32 = 8;
230
+ {
231
+ println! (" {}" , x); // Prints " 8"
232
+ let x = 12;
233
+ println! (" {}" , x); // Prints " 12"
234
+ }
235
+ println! (" {}" , x); // Prints " 8"
236
+ let x = 42;
237
+ println! (" {}" , x); // Prints " 42"
238
+ ` ` `
239
+
240
+ Shadowing and mutable bindings may appear as two sides of the same coin, but
241
+ they are two distinct concepts that can' t always be used interchangeably. For
242
+ one, shadowing enables us to rebind a name to a value of a different type. It
243
+ is also possible to change the mutability of a binding.
244
+
245
+ ```rust
246
+ let mut x: i32 = 1;
247
+ x = 7;
248
+ let x = x; // x is now immutable and is bound to 7
249
+
250
+ let y = 4;
251
+ let y = "I can also be bound to text!"; // y is now of a different type
252
+ ```
0 commit comments