11% Variable Bindings
22
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:
45
5- ``` { rust}
6+ ``` rust
67fn main () {
78 let x = 5 ;
89}
910```
1011
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.
1415
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:
2020
21- ``` { rust}
21+ ``` rust
2222let (x , y ) = (1 , 2 );
2323```
2424
2525After 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
2831
2932Rust 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 checekd 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.
3337
3438We can add the type if we want to, though. Types come after a colon (` : ` ):
3539
36- ``` { rust}
40+ ``` rust
3741let x : i32 = 5 ;
3842```
3943
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 ` .”
4246
4347In this case we chose to represent ` x ` as a 32-bit signed integer. Rust has
4448many different primitive integer types. They begin with ` i ` for signed integers
@@ -48,19 +52,20 @@ bits.
4852In future examples, we may annotate the type in a comment. The examples will
4953look like this:
5054
51- ``` { rust}
55+ ``` rust
5256fn main () {
5357 let x = 5 ; // x: i32
5458}
5559```
5660
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.
6065
6166By default, bindings are * immutable* . This code will not compile:
6267
63- ``` { ignore}
68+ ``` rust, ignore
6469let x = 5;
6570x = 10;
6671```
@@ -75,62 +80,63 @@ error: re-assignment of immutable variable `x`
7580
7681If you want a binding to be mutable, you can use ` mut ` :
7782
78- ``` { rust}
83+ ``` rust
7984let mut x = 5 ; // mut x: i32
8085x = 10 ;
8186```
8287
8388There 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
8590say ` mut ` , the compiler will catch it, and let you know that you have mutated
8691something you may not have intended to mutate. If bindings were mutable by
8792default, the compiler would not be able to tell you this. If you _ did_ intend
8893mutation, then the solution is quite easy: add ` mut ` .
8994
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
9196out of the scope of this guide. In general, you can often avoid explicit
9297mutation, 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.
9499
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
96101differs from other languages: bindings are required to be initialized with a
97102value before you're allowed to use them.
98103
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:
100105
101- ``` { rust}
106+ ``` rust
102107fn main () {
103108 let x : i32 ;
104109
105110 println! (" Hello world!" );
106111}
107112```
108113
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!":
111116
112117``` text
113118 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
115121src/main.rs:2 let x: i32;
116122 ^
117123```
118124
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:
122128
123- ``` { rust,ignore}
129+ ``` rust,ignore
124130fn main() {
125131 let x: i32;
126132
127133 println!("The value of x is: {}", x);
128134}
129135```
130136
131- And try to build it. You' ll get an error:
137+ And try to build it. You’ ll get an error:
132138
133- ``` { bash}
139+ ``` bash
134140$ cargo build
135141 Compiling hello_world v0.0.1 (file:///home/you/projects/hello_world)
136142src/main.rs:4:39: 4:40 error: use of possibly uninitialized variable: ` x`
@@ -144,18 +150,20 @@ error: aborting due to previous error
144150Could not compile ` hello_world` .
145151```
146152
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
148154talk about this stuff we've added to ` println! ` .
149155
150156If you include two curly braces (` {} ` , some call them moustaches...) in your
151157string to print, Rust will interpret this as a request to interpolate some sort
152158of value. * String interpolation* is a computer science term that means "stick
153159in 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