In this module, we're going to be talking
about variables
and especially what makes variables different
in Rust as opposed to other programming
languages.
First, we'll cover the syntax to
declare a variable in Rust.
Then, we'll talk
about mutability, which is whether
a variable is allowed to change its value
or not.
Finally, we'll discuss how every variable
in Rust has a type, whether we explicitly
annotate that type
or not.
You're probably familiar
with variables from other programming languages.
To review, a variable is like a
cardboard box that you can label
and put data into for use later.
Then we write code that operates on the variable
name and doesn't need to know what's actually
in the box until the program is running.
Let's talk about how to declare a variable in Rust.
All variable
declarations start
with the let keyword followed
by the name we want. Here,
we're declaring a variable named x.
To give variables an initial value,
we use an equals sign
and then specify the value we want to
assign to the variable.
Here, the value we are choosing is the number 5.
Semicolons separate Rust statements.
Once we have a variable declared, we can
use it by its name.
Here's an example
with three variables,
and we've built the value of the variable z
using the values in the variables
x and y.
A few more things to note about this code:
the main function is a special function
that's the entry point to binary programs.
It's the function that gets called when we execute
cargo run.
We're going to talk about functions in a future
module.
The println! macro is how you print in Rust.
The first argument is the format string
and the curly braces in that format string
are placeholders for the values we want to
print out, which are specified
as the rest of the arguments.
The Standard Library documentation has a full
format string reference.
If we switch over to the terminal
and execute cargo run for this program,
we'll see z is 11
printed out.
Next, let's talk about mutability.
One thing that's different about variables in Rust
is that, by default,
they can't actually vary.
Here's a program where we've declared a variable
named x with an initial value of
5.
Then, we try to add 1 to the value
and store the result in x.
The red line beneath the code on line 3
is the IDE signaling that we have a
compiler error on that line.
If we build this code in the terminal,
we can see the full compiler error,
which says we cannot assign twice
to the immutable variable, x.
After we assign the value 5 to x,
we're not allowed to change that value.
The reason Rust variables aren't allowed to change
by default is to help prevent some bugs.
Bugs can happen
if you look at a value
and assume that value never changes,
but another part of the program might change
that value and violate your assumptions.
Instead, Rust encodes the assumption
that a value will never change by making
that the default for variables.
To make sure all parts of a program agree
that a variable is allowed to change,
we have to explicitly opt in to allowing mutability.
The way to make a
variable mutable is
with the mut keyword. By
saying let mut x =,
we're then allowed to change the value of x whenever
we want by reassigning to it.
After adding mut to the previous example,
the program compiles without any errors
and prints the value of x is 6.
The last big difference between variables
in Rust and variables in some other languages
is that every variable
in Rust has a type.
These two variables have different types
based on the values they contain.
If we want to annotate the type of a variable,
we can by putting a colon after
the name
and then the type after the equals sign.
In our previous examples,
we didn't have to say that x was an i32,
though. Rust was
able to figure that out based on the value,
5, that we assigned to it.
If you'd like to annotate your types as you're learning,
feel free.
However, most idiomatic Rust
you'll see doesn't annotate types
except for situations in which Rust
can't figure out the type you want without
an annotation.
If you're ever unsure of the type of something
you have, a trick to figure that out
is to annotate the variable
with a type that it definitely isn't.
For example, say we have a variable,
y, that has the value true
and we want to find out the type of the value
true. We know that
it's definitely not an
i32.
We can annotate y as type i32.
The compiler error we get tells us
that the type of true is bool.
In this module,
we've covered how to declare a variable,
how to make a variable mutable
with the mut keyword,
and how every variable has a type.
In the next video,
we'll talk about primitive data types.