Difference between revisions of "Rust-Programming-Language/C2/Variables-and-Mutability/English"
| Line 1: | Line 1: | ||
| − | |||
{| border="1" | {| border="1" | ||
|- | |- | ||
| Line 5: | Line 4: | ||
|| '''Narration''' | || '''Narration''' | ||
|- | |- | ||
| − | || | + | || <div style="color:#000000;">Show Slide: </div> |
| − | '''Title Slide''' | + | <div style="color:#000000;">'''Title Slide'''</div> |
| − | || Welcome to the Spoken Tutorial on '''Variables '''and''' Mutability '''in '''Rust.''' | + | || <span style="color:#000000;">Welcome to the Spoken Tutorial on </span><span style="color:#000000;">'''Variables '''</span><span style="color:#000000;">and</span><span style="color:#000000;">''' Mutability '''</span><span style="color:#000000;">in </span><span style="color:#000000;">'''Rust.'''</span> |
| − | |- | + | |- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;" |
| − | || ''' | + | || '''Slide 2''' |
'''Learning Objective''' | '''Learning Objective''' | ||
|| In this tutorial, we will learn about: | || In this tutorial, we will learn about: | ||
| − | * Variables | + | * <div style="color:#000000;">Variables and</div> |
| − | + | * <div style="color:#000000;">Mutability</div><br/> | |
| − | * | + | |
| − | + | ||
| − | + | ||
| − | + | ||
| + | |- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.189cm;padding-right:0.191cm;" | ||
| + | || '''Slide 3''' | ||
'''System Requirements''' | '''System Requirements''' | ||
| − | || | + | || T<span style="color:#000000;">his tutorial is recorded using</span> |
| − | * '''Ubuntu''' '''Linux OS version 22.04''' | + | * <div style="margin-left:1.27cm;margin-right:0cm;"><span style="color:#000000;">'''Ubuntu'''</span><span style="color:#000000;"> </span><span style="color:#000000;">'''Linux OS version 22.04'''</span></div> |
| − | * '''Rust 1.80.0 ''' | + | * <div style="margin-left:1.27cm;margin-right:0cm;"><span style="color:#000000;">'''Rust 1.</span>80<span style="color:#000000;">.0 '''</span></div> |
| − | * '''Visual | + | * <div style="margin-left:1.27cm;margin-right:0cm;"><span style="color:#000000;">'''Visual </span>S<span style="color:#000000;">tudio </span>C<span style="color:#000000;">ode 1.19.0'''</span></div> |
| − | + | ||
| − | + | ||
| + | |- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.189cm;padding-right:0.191cm;" | ||
| + | || '''Slide 4''' | ||
'''Prerequisites''' | '''Prerequisites''' | ||
| − | |||
| − | |||
|| To follow this tutorial, | || To follow this tutorial, | ||
| − | * You should be familiar with compiling and running a '''Rust''' program | + | * <div style="margin-left:1.27cm;margin-right:0cm;"><span style="color:#000000;">You should be familiar with compiling and running a </span><span style="color:#000000;">'''Rust'''</span><span style="color:#000000;"> </span>program</div> |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| + | <div style="color:#000000;"></div> | ||
| + | |- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.189cm;padding-right:0.191cm;" | ||
| + | || '''Slide 5''' | ||
'''Code Files''' | '''Code Files''' | ||
| − | || | + | || |
| − | * The | + | * The following code file required to practise this tutorial |
| − | + | * This file is provided in the Code Files link of this tutorial page | |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| + | |- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;" | ||
| + | || '''Slide 6''' | ||
'''Variables''' | '''Variables''' | ||
let a = 10; //immutable | let a = 10; //immutable | ||
let mut b=15;//mutable | let mut b=15;//mutable | ||
|| | || | ||
| − | * In Rust, we create variables using the '''let''' statement. | + | * <div style="margin-left:1.27cm;margin-right:0cm;">In Rust, we create variables using the '''let''' statement.</div> |
| − | * By default, variables are immutable, meaning their values cannot be changed | + | * <div style="margin-left:1.27cm;margin-right:0cm;">By default, variables are immutable,meaning their values cannot be changed</div> |
| − | * You can make them mutable by using '''mut'''. | + | * <div style="margin-left:1.27cm;margin-right:0cm;">You can make them mutable by using '''mut'''.</div> |
| − | |- | + | |- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;" |
|| Open '''Visual Studio Code '''editor | || Open '''Visual Studio Code '''editor | ||
|| Let us open the '''Visual Studio Code editor'''. | || Let us open the '''Visual Studio Code editor'''. | ||
| − | |- | + | |- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;" |
|| Open the terminal by pressing | || Open the terminal by pressing | ||
| − | Ctrl+Tilde (~) | + | <div style="color:#000000;">Ctrl+Tilde (~)</div> |
Click on''' Terminal''' and select''' New Terminal.''' | Click on''' Terminal''' and select''' New Terminal.''' | ||
| − | || In the menu bar, click on ''' Terminal''' and select''' New Terminal.''' | + | || In the menu bar, click on''' Terminal''' and select''' New Terminal.''' |
We can see a terminal window at the bottom. | We can see a terminal window at the bottom. | ||
| − | |- | + | |- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;" |
| − | || Type at the prompt: | + | || |
| + | |||
| + | <div style="color:#ff0000;"></div> | ||
| + | Type at the prompt: | ||
'''cargo new variables''' | '''cargo new variables''' | ||
| Line 79: | Line 74: | ||
|| Go to our working directory '''MyRustProject''' as explained earlier. | || Go to our working directory '''MyRustProject''' as explained earlier. | ||
| − | Please refer to the '''Additional Reading material ''' link of this tutorial | + | <div style="color:#000000;">Please refer to the '''Additional Reading material '''link of this tutorial.</div> |
| − | + | ||
It explains the steps to create and run the project. | It explains the steps to create and run the project. | ||
| − | Type the command '''cargo new variables ''' and press Enter | + | Type the command '''cargo new variables '''and press Enter |
Open the created project as shown. | Open the created project as shown. | ||
| − | |- | + | |- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;" |
| − | || Point to''' main.rs file.''' | + | || <span style="color:#252525;">Point to</span><span style="color:#252525;">''' main.rs file.'''</span> |
| − | '''Press Ctrl + C to copy ''' | + | <div style="color:#252525;">'''Press Ctrl + C to copy '''</div> |
| − | '''Press Ctrl + V to paste''' | + | <div style="color:#252525;">'''Press Ctrl + V to paste'''</div> |
| + | || <span style="background-color:#ffffff;color:#252525;">In the </span><span style="background-color:#ffffff;color:#252525;">'''main.rs '''</span><span style="background-color:#ffffff;color:#252525;">file,</span><span style="background-color:#ffffff;color:#252525;"> copy and paste the</span><span style="background-color:#ffffff;color:#252525;"> </span><span style="background-color:#ffffff;color:#252525;">'''code'''</span><span style="background-color:#ffffff;color:#252525;"> </span><span style="background-color:#ffffff;color:#252525;">from the codefile.</span> | ||
| + | |- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;" | ||
| + | || <div style="color:#000000;">Highlight the code according to narration</div> | ||
| − | + | <div style="color:#252525;">fn main(){</div> | |
| − | + | ||
| − | + | ||
| − | + | <div style="color:#252525;">let a = 1;</div> | |
| − | let a = 1; | + | |
| − | println!(“The value of a is {}”,a); | + | <div style="color:#252525;">println!(“The value of a is {}”,a);</div> |
| − | a=2; | + | |
| − | println!(“The value of a is {}”,a); | + | <div style="color:#252525;">a=2;</div> |
| − | } | + | |
| − | || '''let''' keyword is used to declare variables in ''' | + | <div style="color:#252525;">println!(“The value of a is {}”,a);</div> |
| + | |||
| + | <div style="color:#252525;">}</div> | ||
| + | || <span style="color:#252525;">'''let'''</span><span style="color:#252525;"> keyword is used to declare variables in </span><span style="color:#252525;">'''Rus'''</span><span style="color:#252525;">t.</span> | ||
| − | Here we have initialized the variable '''a''' and assigned the value 1 to it. | + | <span style="color:#252525;">Here we have initialized the variable </span><span style="color:#252525;">'''a'''</span><span style="color:#252525;"> and assigned the value 1 to it.</span> |
| − | + | <span style="color:#252525;">W</span><span style="color:#252525;">e are trying to reassign the value 2 to variable </span><span style="color:#252525;">'''a</span><span style="color:#252525;">.'''</span> | |
| − | After reassigning we are printing the variable’s value to see what the value contains. | + | <span style="color:#252525;">After reassigning we are printing the variable’s value to see </span><span style="color:#252525;">what the</span><span style="color:#252525;"> value </span><span style="color:#252525;">contains</span><span style="color:#252525;">.</span> |
| − | Press '''Ctrl ''' and ''' S '''to | + | <span style="color:#252525;">Press </span><span style="color:#252525;">'''Ctrl '''</span><span style="color:#252525;">and</span><span style="color:#252525;">''' S '''</span><span style="color:#252525;">to s</span><span style="color:#252525;">ave the file.</span> |
| − | |- | + | |- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;" |
| − | || | + | | style="color:#252525;" | |
|| In the menu bar, click on terminal and select '''New Terminal.''' | || In the menu bar, click on terminal and select '''New Terminal.''' | ||
| − | |- | + | |- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;" |
| − | || Type | + | || <span style="color:#252525;">Type</span><span style="color:#252525;"> </span><span style="color:#252525;">Cargo build</span> |
| − | || In the terminal, type '''cargo build '''to compile the Cargo project. | + | || <span style="color:#252525;">In the terminal, </span><span style="color:#252525;">type </span><span style="color:#252525;">'''cargo build</span><span style="color:#252525;"> '''</span><span style="color:#252525;">to compile the Cargo project.</span> |
| − | |- | + | |- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;" |
| − | || Highlight the error | + | | style="color:#252525;" | Highlight the error |
| − | || Here, we can see an error - '''cannot assign twice to immutable variable ‘a’.''' | + | || <div style="color:#252525;">Here, we can see an error - '''cannot assign twice to immutable variable ‘a’.'''</div> |
| − | Note that we had mentioned '''variables ''' are '''immutable ''' by default. | + | <span style="color:#252525;">Note that we </span><span style="color:#252525;">had mentioned </span><span style="color:#252525;">'''variables '''</span><span style="color:#252525;">are </span><span style="color:#252525;">'''immutable '''</span><span style="color:#252525;">by default.</span> |
| − | |- | + | |- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;" |
| − | || | + | | style="color:#252525;" | |
|| So switch back to the '''program.''' | || So switch back to the '''program.''' | ||
| − | |- | + | |- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;" |
| − | || Type mut | + | || <div style="color:#252525;">Type mut</div> |
| − | Let mut a=1; | + | <div style="color:#252525;">Let mut a=1;</div> |
| − | || Now, type '''mut''' in between '''let''' and '''a.''' | + | || <span style="color:#252525;">Now, type </span><span style="color:#252525;">'''mut'''</span><span style="color:#252525;"> in between </span><span style="color:#252525;">'''let'''</span><span style="color:#252525;"> and </span><span style="color:#252525;">'''a.'''</span> |
| − | Here we are telling the compiler that the initialized variable is mutable. | + | <div style="color:#252525;">Here we are telling the compiler that the initialized variable is mutable.</div> |
| − | |- | + | |- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;" |
| − | || Type cargo run | + | | style="color:#252525;" | Type cargo run |
|| Save the file. | || Save the file. | ||
| − | In the terminal, type '''cargo run''' | + | In the terminal, type<span style="color:#252525;"> </span><span style="color:#252525;">'''cargo run'''</span> |
| − | |- | + | |- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;" |
| − | || Highlight the output | + | | style="color:#252525;" | Highlight the output |
| − | || We can see the output. | + | || <div style="color:#252525;">We can see the output.</div> |
| − | Both the '''initialized ''' and the modified values are printed successfully. | + | <span style="color:#252525;">Both the </span><span style="color:#252525;">'''initialized '''</span><span style="color:#252525;">and the modified values are printed successfully.</span> |
| − | |- | + | |- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;" |
| − | || | + | | style="color:#252525;" | |
|| Next let us see the shadowing of variables. | || Next let us see the shadowing of variables. | ||
| − | |- | + | |- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;" |
|| '''Shadowing''' | || '''Shadowing''' | ||
| Line 154: | Line 152: | ||
'''let x = 5;''' | '''let x = 5;''' | ||
'''println!("x is {}", x);''' | '''println!("x is {}", x);''' | ||
| − | |||
'''let x = x + 2;''' | '''let x = x + 2;''' | ||
'''println!("x is {}", x);''' | '''println!("x is {}", x);''' | ||
'''}''' | '''}''' | ||
| − | |||
| − | |||
|| Clear the code window and then copy and paste the code from the code file. | || Clear the code window and then copy and paste the code from the code file. | ||
| Line 172: | Line 167: | ||
Save the file. | Save the file. | ||
| − | |- | + | |- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;" |
| − | || | + | || |
| − | || In the terminal, type '''cargo run''' | + | || In the terminal, type<span style="color:#252525;"> </span><span style="color:#252525;">'''cargo run'''</span> |
Check the output. | Check the output. | ||
| − | It prints the value of | + | It prints the value of x as 5 and 7 as the result of shadowing. |
| − | |- | + | |- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;" |
|| | || | ||
|| Next we will see the scope of the variables. | || Next we will see the scope of the variables. | ||
| − | |- | + | |- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;" |
|| '''Scope of the variable''' | || '''Scope of the variable''' | ||
| Line 212: | Line 207: | ||
|| Clear the code window. | || Clear the code window. | ||
| − | Copy and paste the code from the | + | Copy and paste the code from the codefile. |
| − | In this code, observe the declaration of variables '''outer_var ''' and ''' inner_var.''' | + | In this code, observe the declaration of variables '''outer_var '''and''' inner_var.''' |
| − | Curly braces { } define the block scope where the variable access becomes restricted to | + | Curly braces { } define the block scope where the variable access becomes restricted to local. |
Save the program. | Save the program. | ||
| − | |- | + | |- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;" |
| − | || | + | || <div style="color:#000000;"></div> |
| − | + | ||
| − | + | <div style="color:#000000;"></div> | |
| + | || <div style="color:#000000;">Let us check the output. Run the program.</div> | ||
| − | + | <div style="color:#000000;">We can see a compilation error.</div> | |
| − | + | <div style="color:#000000;">Here we tried to print the '''inner_var '''outside of the inner code block.</div> | |
| − | + | <div style="color:#000000;">So the program will give an error.</div> | |
| − | + | <div style="color:#000000;">Let us comment the print statement of '''inner_var''' in the outer block.</div> | |
| − | + | <div style="color:#000000;">Save the program.</div> | |
| − | + | <div style="color:#000000;">Run the program again to see the output.</div> | |
| − | + | <div style="color:#000000;">We can see the output displayed for '''outer_var '''as 100 and '''inner_var''' as 200.</div> | |
| − | + | <div style="color:#000000;">This shows that any variable outside the braces will have global access.</div> | |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| + | |- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;" | ||
| + | || Slide: | ||
| − | + | Summary | |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
|| This brings us to the end of this tutorial. | || This brings us to the end of this tutorial. | ||
Let us summarize. | Let us summarize. | ||
| − | |- | + | |- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;" |
| − | || | + | || Slide: |
| − | + | Assignment | |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | fn main() { | |
| − | + | let x = 10; // Immutable variable | |
| + | println!("The value of x is: {}", x); | ||
| + | x = 20; | ||
| + | println!("The new value of x is: {}", x); | ||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
} | } | ||
| − | |||
|| As an assignment, do the following. | || As an assignment, do the following. | ||
| − | * Run the above program | + | * <div style="margin-left:1.27cm;margin-right:0cm;">Run the above program</div> |
| − | * Analyse the error and correct the program. | + | * <div style="margin-left:1.27cm;margin-right:0cm;">Analyse the error and correct the program.</div> |
|- | |- | ||
| − | | | + | | style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;" | Thank You |
| − | + | We would like to thank '''Vishal Pokuri from VIT Vellore '''for content contribution. | |
| − | + | | style="border:1pt solid #252525;padding-top:0.176cm;padding-bottom:0.176cm;padding-left:0.123cm;padding-right:0.176cm;" | | |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | |- | + | |
| − | | | + | |
| − | + | ||
Thank you for joining. | Thank you for joining. | ||
|- | |- | ||
|} | |} | ||
| + | <div style="margin-left:-1.588cm;margin-right:-1.429cm;"></div> | ||
Revision as of 13:48, 14 July 2025
| Visual Cue | Narration |
| Show Slide:
Title Slide
|
Welcome to the Spoken Tutorial on Variables and Mutability in Rust. |
| Slide 2
Learning Objective |
In this tutorial, we will learn about:
|
| Slide 3
System Requirements |
This tutorial is recorded using
|
| Slide 4
Prerequisites |
To follow this tutorial,
|
| Slide 5
Code Files |
|
| Slide 6
Variables let a = 10; //immutable let mut b=15;//mutable |
|
| Open Visual Studio Code editor | Let us open the Visual Studio Code editor. |
| Open the terminal by pressing
Ctrl+Tilde (~)
Click on Terminal and select New Terminal. |
In the menu bar, click on Terminal and select New Terminal.
We can see a terminal window at the bottom. |
|
Type at the prompt: cargo new variables |
Go to our working directory MyRustProject as explained earlier.
Please refer to the Additional Reading material link of this tutorial.
It explains the steps to create and run the project. Type the command cargo new variables and press Enter Open the created project as shown. |
| Point to main.rs file.
Press Ctrl + C to copy
Press Ctrl + V to paste
|
In the main.rs file, copy and paste the code from the codefile. |
| Highlight the code according to narration
fn main(){
let a = 1;
println!(“The value of a is {}”,a);
a=2;
println!(“The value of a is {}”,a);
}
|
let keyword is used to declare variables in Rust.
Here we have initialized the variable a and assigned the value 1 to it. We are trying to reassign the value 2 to variable a. After reassigning we are printing the variable’s value to see what the value contains. Press Ctrl and S to save the file. |
| In the menu bar, click on terminal and select New Terminal. | |
| Type Cargo build | In the terminal, type cargo build to compile the Cargo project. |
| Highlight the error | Here, we can see an error - cannot assign twice to immutable variable ‘a’.
Note that we had mentioned variables are immutable by default. |
| So switch back to the program. | |
| Type mut
Let mut a=1;
|
Now, type mut in between let and a.
Here we are telling the compiler that the initialized variable is mutable.
|
| Type cargo run | Save the file.
In the terminal, type cargo run |
| Highlight the output | We can see the output.
Both the initialized and the modified values are printed successfully. |
| Next let us see the shadowing of variables. | |
| Shadowing
fn main() { let x = 5; println!("x is {}", x); let x = x + 2; println!("x is {}", x); } |
Clear the code window and then copy and paste the code from the code file.
Rust allows variable shadowing. Here the variable x is shadowing. Shadowing is to declare a new variable with the same name as a previous variable in the same scope. We can assign a new value to the new variable while the old variable remains unchanged. Save the file. |
| In the terminal, type cargo run
Check the output. It prints the value of x as 5 and 7 as the result of shadowing. | |
| Next we will see the scope of the variables. | |
| Scope of the variable
fn main() { // scope of outer_var variable is inside the main function code block let outer_var = 100; // start of the inner code block { // scope of inner_var variable is only inside this new code block let inner_var = 200; println!("inner_var = {}", inner_var); } // end of the inner code block println!("inner_var = {}", inner_var); println!("outer_var = {}", outer_var); } |
Clear the code window.
Copy and paste the code from the codefile. In this code, observe the declaration of variables outer_var and inner_var. Curly braces { } define the block scope where the variable access becomes restricted to local. Save the program. |
| Let us check the output. Run the program.
We can see a compilation error.
Here we tried to print the inner_var outside of the inner code block.
So the program will give an error.
Let us comment the print statement of inner_var in the outer block.
Save the program.
Run the program again to see the output.
We can see the output displayed for outer_var as 100 and inner_var as 200.
This shows that any variable outside the braces will have global access.
| |
| Slide:
Summary |
This brings us to the end of this tutorial.
Let us summarize. |
| Slide:
Assignment fn main() { let x = 10; // Immutable variable println!("The value of x is: {}", x); x = 20; println!("The new value of x is: {}", x); } |
As an assignment, do the following.
|
| Thank You
We would like to thank Vishal Pokuri from VIT Vellore for content contribution. |
Thank you for joining. |