hey there Leo here today you're going to
learn everything that you need to know
when getting started using variables and
rust you're going to learn how to
declare variables what shadowing is what
mutability is what type annotation is
and at the very end of this video you're
going to be tested on that knowledge so
let's get coding in Rust you declare
variables using the let keyword followed
by the variable name and optionally a
type annotation I'm a big fan of
examples so let's check this out now say
let score colon I8 = 10 you might say
what is this I8 and what is this type
annotation well every variable has a
type and rust must be able to determine
the type you're using if you don't
explicitly set it in this code the I8 is
an 8bit signed integer type and if this
sounds confusing don't worry we're going
to cover types in the next video and if
I go ahead and delete this you'll
actually notice this thing called a type
inlay this is from installing rust
analyzer in the previous video that
actually
implicitly create score as an i32 or a
sin 32bit integer again something that
you don't have to know right now and
that we'll cover in the next video but
what happens if we try to add to this
value score let's say someone scored
another point so score equals score + 1
we can see here that it's already giving
us an error and the reason there's an
error is Sayes cannot mutate immutable
variable score and that's something to
note in Rust variables are immutable by
default and in other words once a value
is bound to a variable it cannot be
changed you might think this is strange
coming from a different language but
immutable by default has a ton of
benefits which we'll get into later but
for now how do we get around this
problem well rust allows you to Shadow a
variable by declaring a new variable
with the same name in the same scope
this effectively masked the previous
variable let's go ahead and do this
now say let score equals 11 and notice
we're even changing the type here so
previously it was an i32 and now it's a
string slice let's print out the score
just to be certain we understand what's
happening and we see the score is 11
shadowing lets you change the type of a
variable or provide a more refined value
within a nested scope so we can actually
adjust the scope here by adding
parentheses so what do you think we're
going to get here if you said 11 and and
then 10 you'd be absolutely right we see
the inner scope score is 11 the outer
scope score is 10 we can also delete
this Shadow here and now what do you
think we'll get if you said two tens in
a row you're absolutely right but what
happens if we want to actually change
the variable if I say score equal score
+ 1 I get an error unless we make this
mutable using the mute
keyword and now when I I go ahead and
run this we'll see the score is 10 and
then the score is 11 we can also create
something called a constant and
constants are like immutable variables
but they must be declared with the con
keyword and their values must be known
as compile time contrast this with
immutable variables that can have values
determined at runtime but cannot be
changed after initialization you might
use constants for something like the max
score so let's see this in action here
const Max score i32 = 100 and now I can
use or print out this Max score wherever
I want to so I'm going to copy
this paste this
the add Max score out
here and let's run
it so we see the inner scope score is 10
the outer scope score is 11 and the max
score is 100 and notice that the
constant is a global declared outside of
the main function so what are the key
points you use let to declare variables
and variables are immutable by default
for safety and predictability reasons
use mute to make variables mutable or
changeable and use const for values that
never change and that are known at
compile time okay now it's time to test
our knowledge using rustlings rustlings
provides small exercises to get used to
reading and writing code so let's start
with variables one we see here a main
function x = 5 and then the print line
macro with X has the value we have a
placeholder here and then the variable X
which will use string interpolation to
essentially substitute this placeholder
with the value of x but what's the
problem why are we getting this nasty
error message if you said it's because
we don't have the let keyword you're
correct let's go ahead and run that we
say perfect it ran so I'm going to go
ahead and remove I'm not done and we'll
get to the next one
okay so what's going on here we have the
main function let X if xal equal 10 x is
10 else it's not 10 well the problem is
X doesn't have a value so if I say x let
x equal 10 and save
it it will compile so X is 10 you can
also make it
11 and that will make it go to the else
and we'll see that X is not 10 Okay now
what's going on with variables 3 we have
the main function let x equals then i32
and they're trying to print it well you
can't print something that's not
initialized so we need to set it to some
type of i32 so we'll say
10 and that worked
perfect okay now let's go to number
four we see a main function let x = 3
then we print the number X we then set x
= 5 and we're instructed not to change
this line and then we print it again so
we can't Shadow because it says don't
change this line so we need to make this
mutable we save it and we then print out
the number three the number of five and
that works okay let's see what we've got
here here we have a string literal which
is three that we bind to a string slice
number and then we print it out and then
we set number equal to three you may
think we could make this mutable but
that would throw an error because rust
is statically typed and the type of
every variable must be known at compile
time and cannot change at runtime
instead what we need to do is Shadow
number by adding let to the Declaration
so let number equals three save that and
the code is compiling okay one more to
go in variable six we see we have this
constant number equals 3 and they're
trying to print it out if you remember
from the earlier lesson constants need
to be annotated so we could just make
this an I8 i32 whatever we want and save
this and we're done great job if you
were able to follow all this you're able
to declare variables you understand
shadowing you understand mutability you
understand constants and in the next
video you're going to learn all about
the various types in Rust
that