1
1
% Variable Bindings
2
2
3
- The first thing we'll learn about are * variable bindings* . They look like this:
3
+ Vitually every non-’Hello World’ Rust program uses * variable bindings* . They
4
+ look like this:
4
5
5
- ``` { rust}
6
+ ``` rust
6
7
fn main () {
7
8
let x = 5 ;
8
9
}
9
10
```
10
11
11
- Putting ` fn main() { ` in each example is a bit tedious, so we' ll leave that out
12
- in the future. If you' re following along, make sure to edit your ` main() `
13
- function, rather than leaving it off. Otherwise, you' ll get an error.
12
+ Putting ` fn main() { ` in each example is a bit tedious, so we’ ll leave that out
13
+ in the future. If you’ re following along, make sure to edit your ` main() `
14
+ function, rather than leaving it off. Otherwise, you’ ll get an error.
14
15
15
- In many languages, this is called a * variable* . But Rust's variable bindings
16
- have a few tricks up their sleeves. Rust has a very powerful feature called
17
- * pattern matching* that we'll get into detail with later, but the left
18
- hand side of a ` let ` expression is a full pattern, not just a variable name.
19
- This means we can do things like:
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:
20
20
21
- ``` { rust}
21
+ ``` rust
22
22
let (x , y ) = (1 , 2 );
23
23
```
24
24
25
25
After this expression is evaluated, ` x ` will be one, and ` y ` will be two.
26
- Patterns are really powerful, but this is about all we can do with them so far.
27
- So let's just keep this in the back of our minds as we go forward.
26
+ Patterns are really powerful, and have [ their own section] [ pattern ] in the
27
+ book. We don’t need those features for now, so we’ll just keep this in the back
28
+ of our minds as we go forward.
29
+
30
+ [ pattern ] : patterns.html
28
31
29
32
Rust is a statically typed language, which means that we specify our types up
30
- front. So why does our first example compile? Well, Rust has this thing called
31
- * type inference* . If it can figure out what the type of something is, Rust
32
- doesn't require you to actually type it out.
33
+ front, and they’re checked at compile time. So why does our first example
34
+ compile? Well, Rust has this thing called ‘type inference’. If it can figure
35
+ out what the type of something is, Rust doesn’t require you to actually type it
36
+ out.
33
37
34
38
We can add the type if we want to, though. Types come after a colon (` : ` ):
35
39
36
- ``` { rust}
40
+ ``` rust
37
41
let x : i32 = 5 ;
38
42
```
39
43
40
- If I asked you to read this out loud to the rest of the class, you' d say " ` x `
41
- is a binding with the type ` i32 ` and the value ` five ` ."
44
+ If I asked you to read this out loud to the rest of the class, you’ d say “ ` x `
45
+ is a binding with the type ` i32 ` and the value ` five ` .”
42
46
43
47
In this case we chose to represent ` x ` as a 32-bit signed integer. Rust has
44
48
many different primitive integer types. They begin with ` i ` for signed integers
@@ -48,19 +52,20 @@ bits.
48
52
In future examples, we may annotate the type in a comment. The examples will
49
53
look like this:
50
54
51
- ``` { rust}
55
+ ``` rust
52
56
fn main () {
53
57
let x = 5 ; // x: i32
54
58
}
55
59
```
56
60
57
- Note the similarities between this annotation and the syntax you use with ` let ` .
58
- Including these kinds of comments is not idiomatic Rust, but we'll occasionally
59
- include them to help you understand what the types that Rust infers are.
61
+ Note the similarities between this annotation and the syntax you use with
62
+ ` let ` . Including these kinds of comments is not idiomatic Rust, but we'll
63
+ occasionally include them to help you understand what the types that Rust
64
+ infers are.
60
65
61
66
By default, bindings are * immutable* . This code will not compile:
62
67
63
- ``` { ignore}
68
+ ``` rust, ignore
64
69
let x = 5;
65
70
x = 10;
66
71
```
@@ -75,62 +80,63 @@ error: re-assignment of immutable variable `x`
75
80
76
81
If you want a binding to be mutable, you can use ` mut ` :
77
82
78
- ``` { rust}
83
+ ``` rust
79
84
let mut x = 5 ; // mut x: i32
80
85
x = 10 ;
81
86
```
82
87
83
88
There is no single reason that bindings are immutable by default, but we can
84
- think about it through one of Rust' s primary focuses: safety. If you forget to
89
+ think about it through one of Rust’ s primary focuses: safety. If you forget to
85
90
say ` mut ` , the compiler will catch it, and let you know that you have mutated
86
91
something you may not have intended to mutate. If bindings were mutable by
87
92
default, the compiler would not be able to tell you this. If you _ did_ intend
88
93
mutation, then the solution is quite easy: add ` mut ` .
89
94
90
- There are other good reasons to avoid mutable state when possible, but they' re
95
+ There are other good reasons to avoid mutable state when possible, but they’ re
91
96
out of the scope of this guide. In general, you can often avoid explicit
92
97
mutation, and so it is preferable in Rust. That said, sometimes, mutation is
93
- what you need, so it' s not verboten.
98
+ what you need, so it’ s not verboten.
94
99
95
- Let' s get back to bindings. Rust variable bindings have one more aspect that
100
+ Let’ s get back to bindings. Rust variable bindings have one more aspect that
96
101
differs from other languages: bindings are required to be initialized with a
97
102
value before you're allowed to use them.
98
103
99
- Let' s try it out. Change your ` src/main.rs ` file to look like this:
104
+ Let’ s try it out. Change your ` src/main.rs ` file to look like this:
100
105
101
- ``` { rust}
106
+ ``` rust
102
107
fn main () {
103
108
let x : i32 ;
104
109
105
110
println! (" Hello world!" );
106
111
}
107
112
```
108
113
109
- You can use ` cargo build ` on the command line to build it. You' ll get a warning,
110
- but it will still print "Hello, world!":
114
+ You can use ` cargo build ` on the command line to build it. You’ ll get a
115
+ warning, but it will still print "Hello, world!":
111
116
112
117
``` text
113
118
Compiling hello_world v0.0.1 (file:///home/you/projects/hello_world)
114
- src/main.rs:2:9: 2:10 warning: unused variable: `x`, #[warn(unused_variable)] on by default
119
+ src/main.rs:2:9: 2:10 warning: unused variable: `x`, #[warn(unused_variable)]
120
+ on by default
115
121
src/main.rs:2 let x: i32;
116
122
^
117
123
```
118
124
119
- Rust warns us that we never use the variable binding, but since we never use it,
120
- no harm, no foul. Things change if we try to actually use this ` x ` , however. Let's
121
- do that. Change your program to look like this:
125
+ Rust warns us that we never use the variable binding, but since we never use
126
+ it, no harm, no foul. Things change if we try to actually use this ` x ` ,
127
+ however. Let’s do that. Change your program to look like this:
122
128
123
- ``` { rust,ignore}
129
+ ``` rust,ignore
124
130
fn main() {
125
131
let x: i32;
126
132
127
133
println!("The value of x is: {}", x);
128
134
}
129
135
```
130
136
131
- And try to build it. You' ll get an error:
137
+ And try to build it. You’ ll get an error:
132
138
133
- ``` { bash}
139
+ ``` bash
134
140
$ cargo build
135
141
Compiling hello_world v0.0.1 (file:///home/you/projects/hello_world)
136
142
src/main.rs:4:39: 4:40 error: use of possibly uninitialized variable: ` x`
@@ -144,18 +150,20 @@ error: aborting due to previous error
144
150
Could not compile ` hello_world` .
145
151
```
146
152
147
- Rust will not let us use a value that has not been initialized. Next, let' s
153
+ Rust will not let us use a value that has not been initialized. Next, let’ s
148
154
talk about this stuff we've added to ` println! ` .
149
155
150
156
If you include two curly braces (` {} ` , some call them moustaches...) in your
151
157
string to print, Rust will interpret this as a request to interpolate some sort
152
158
of value. * String interpolation* is a computer science term that means "stick
153
159
in the middle of a string." We add a comma, and then ` x ` , to indicate that we
154
- want ` x ` to be the value we're interpolating. The comma is used to separate
155
- arguments we pass to functions and macros, if you're passing more than one.
156
-
157
- When you just use the curly braces, Rust will attempt to display the
158
- value in a meaningful way by checking out its type. If you want to specify the
159
- format in a more detailed manner, there are a [ wide number of options
160
- available] ( ../std/fmt/index.html ) . For now, we'll just stick to the default:
161
- integers aren't very complicated to print.
160
+ want ` x ` to be the value we’re interpolating. The comma is used to separate
161
+ arguments we pass to functions and macros, if you’re passing more than one.
162
+
163
+ When you just use the curly braces, Rust will attempt to display the value in a
164
+ meaningful way by checking out its type. If you want to specify the format in a
165
+ more detailed manner, there are a [ wide number of options available] [ format ] .
166
+ For now, we'll just stick to the default: integers aren't very complicated to
167
+ print.
168
+
169
+ [ format ] : ../std/fmt/index.html
0 commit comments