Learn Rust Easily - Variables - Lesson 2
1K views
Dec 11, 2024
Learn how to declare, shadow, and mutate variables in Rust. Test your knowledge at the end with the quizes. 👍 Subscribe for more: https://bit.ly/3lLybeP ⏱️ Video Chapters 0:00 Intro to Variables in Rust 0:15 Declaring Variables 1:43 Shaowding Variables 2:49 Mutating Variables 3:23 Constant Variables 4:25 Testing Your Rust Variables Knowledge 🏷️ Tags #programming #rustlang --- Videos are intended strictly for information, education, and entertainment purposes. Do not base any investment decisions based upon these materials. Please read the disclaimer: https://analyzingalpha.com/disclaimer ---
View Video Transcript
0:00
hey there Leo here today you're going to
0:01
learn everything that you need to know
0:03
when getting started using variables and
0:04
rust you're going to learn how to
0:06
declare variables what shadowing is what
0:08
mutability is what type annotation is
0:10
and at the very end of this video you're
0:12
going to be tested on that knowledge so
0:14
let's get coding in Rust you declare
0:17
variables using the let keyword followed
0:19
by the variable name and optionally a
0:21
type annotation I'm a big fan of
0:23
examples so let's check this out now say
0:26
let score colon I8 = 10 you might say
0:30
what is this I8 and what is this type
0:33
annotation well every variable has a
0:35
type and rust must be able to determine
0:38
the type you're using if you don't
0:39
explicitly set it in this code the I8 is
0:43
an 8bit signed integer type and if this
0:45
sounds confusing don't worry we're going
0:47
to cover types in the next video and if
0:50
I go ahead and delete this you'll
0:53
actually notice this thing called a type
0:55
inlay this is from installing rust
0:57
analyzer in the previous video that
0:59
actually
1:00
implicitly create score as an i32 or a
1:04
sin 32bit integer again something that
1:06
you don't have to know right now and
1:08
that we'll cover in the next video but
1:11
what happens if we try to add to this
1:13
value score let's say someone scored
1:15
another point so score equals score + 1
1:18
we can see here that it's already giving
1:20
us an error and the reason there's an
1:22
error is Sayes cannot mutate immutable
1:24
variable score and that's something to
1:26
note in Rust variables are immutable by
1:29
default and in other words once a value
1:31
is bound to a variable it cannot be
1:33
changed you might think this is strange
1:35
coming from a different language but
1:36
immutable by default has a ton of
1:38
benefits which we'll get into later but
1:40
for now how do we get around this
1:42
problem well rust allows you to Shadow a
1:45
variable by declaring a new variable
1:47
with the same name in the same scope
1:50
this effectively masked the previous
1:52
variable let's go ahead and do this
1:55
now say let score equals 11 and notice
1:59
we're even changing the type here so
2:01
previously it was an i32 and now it's a
2:04
string slice let's print out the score
2:06
just to be certain we understand what's
2:13
happening and we see the score is 11
2:17
shadowing lets you change the type of a
2:19
variable or provide a more refined value
2:21
within a nested scope so we can actually
2:24
adjust the scope here by adding
2:25
parentheses so what do you think we're
2:27
going to get here if you said 11 and and
2:29
then 10 you'd be absolutely right we see
2:32
the inner scope score is 11 the outer
2:35
scope score is 10 we can also delete
2:37
this Shadow here and now what do you
2:40
think we'll get if you said two tens in
2:43
a row you're absolutely right but what
2:46
happens if we want to actually change
2:48
the variable if I say score equal score
2:52
+ 1 I get an error unless we make this
2:56
mutable using the mute
2:58
keyword and now when I I go ahead and
3:00
run this we'll see the score is 10 and
3:04
then the score is 11 we can also create
3:07
something called a constant and
3:08
constants are like immutable variables
3:10
but they must be declared with the con
3:12
keyword and their values must be known
3:14
as compile time contrast this with
3:17
immutable variables that can have values
3:18
determined at runtime but cannot be
3:20
changed after initialization you might
3:23
use constants for something like the max
3:25
score so let's see this in action here
3:27
const Max score i32 = 100 and now I can
3:33
use or print out this Max score wherever
3:36
I want to so I'm going to copy
3:38
this paste this
3:42
the add Max score out
3:45
here and let's run
3:48
it so we see the inner scope score is 10
3:51
the outer scope score is 11 and the max
3:53
score is 100 and notice that the
3:55
constant is a global declared outside of
3:58
the main function so what are the key
4:00
points you use let to declare variables
4:03
and variables are immutable by default
4:05
for safety and predictability reasons
4:07
use mute to make variables mutable or
4:11
changeable and use const for values that
4:14
never change and that are known at
4:16
compile time okay now it's time to test
4:19
our knowledge using rustlings rustlings
4:21
provides small exercises to get used to
4:24
reading and writing code so let's start
4:26
with variables one we see here a main
4:28
function x = 5 and then the print line
4:31
macro with X has the value we have a
4:35
placeholder here and then the variable X
4:37
which will use string interpolation to
4:39
essentially substitute this placeholder
4:41
with the value of x but what's the
4:44
problem why are we getting this nasty
4:45
error message if you said it's because
4:47
we don't have the let keyword you're
4:49
correct let's go ahead and run that we
4:52
say perfect it ran so I'm going to go
4:54
ahead and remove I'm not done and we'll
4:56
get to the next one
5:00
okay so what's going on here we have the
5:03
main function let X if xal equal 10 x is
5:08
10 else it's not 10 well the problem is
5:12
X doesn't have a value so if I say x let
5:16
x equal 10 and save
5:20
it it will compile so X is 10 you can
5:23
also make it
5:25
11 and that will make it go to the else
5:28
and we'll see that X is not 10 Okay now
5:32
what's going on with variables 3 we have
5:34
the main function let x equals then i32
5:38
and they're trying to print it well you
5:40
can't print something that's not
5:42
initialized so we need to set it to some
5:45
type of i32 so we'll say
5:49
10 and that worked
5:53
perfect okay now let's go to number
5:56
four we see a main function let x = 3
6:00
then we print the number X we then set x
6:04
= 5 and we're instructed not to change
6:07
this line and then we print it again so
6:10
we can't Shadow because it says don't
6:12
change this line so we need to make this
6:17
mutable we save it and we then print out
6:20
the number three the number of five and
6:22
that works okay let's see what we've got
6:24
here here we have a string literal which
6:28
is three that we bind to a string slice
6:31
number and then we print it out and then
6:34
we set number equal to three you may
6:38
think we could make this mutable but
6:40
that would throw an error because rust
6:42
is statically typed and the type of
6:44
every variable must be known at compile
6:47
time and cannot change at runtime
6:49
instead what we need to do is Shadow
6:51
number by adding let to the Declaration
6:54
so let number equals three save that and
6:58
the code is compiling okay one more to
7:01
go in variable six we see we have this
7:04
constant number equals 3 and they're
7:06
trying to print it out if you remember
7:07
from the earlier lesson constants need
7:10
to be annotated so we could just make
7:12
this an I8 i32 whatever we want and save
7:15
this and we're done great job if you
7:18
were able to follow all this you're able
7:19
to declare variables you understand
7:21
shadowing you understand mutability you
7:24
understand constants and in the next
7:25
video you're going to learn all about
7:27
the various types in Rust
7:44
that