Difference between revisions of "Rust-Programming-Language/C2/Variables-and-Mutability/English"
| (4 intermediate revisions by 2 users not shown) | |||
| Line 1: | Line 1: | ||
| − | |||
{| border="1" | {| border="1" | ||
|- | |- | ||
| Line 5: | Line 4: | ||
|| '''Narration''' | || '''Narration''' | ||
|- | |- | ||
| − | || | + | || Show Slide: |
'''Title Slide''' | '''Title Slide''' | ||
|| Welcome to the Spoken Tutorial on '''Variables '''and''' Mutability '''in '''Rust.''' | || Welcome to the Spoken Tutorial on '''Variables '''and''' Mutability '''in '''Rust.''' | ||
|- | |- | ||
| − | || ''' | + | || '''Slide 2''' |
'''Learning Objective''' | '''Learning Objective''' | ||
|| In this tutorial, we will learn about: | || In this tutorial, we will learn about: | ||
| − | * Variables | + | * Variables and |
| − | * Mutability | + | * Mutability |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| + | |- | ||
| + | || '''Slide 3''' | ||
'''System Requirements''' | '''System Requirements''' | ||
| Line 27: | Line 24: | ||
* '''Rust 1.80.0 ''' | * '''Rust 1.80.0 ''' | ||
* '''Visual Studio Code 1.19.0''' | * '''Visual Studio Code 1.19.0''' | ||
| − | |||
| − | |||
| + | |- | ||
| + | || '''Slide 4''' | ||
'''Prerequisites''' | '''Prerequisites''' | ||
| − | |||
| − | |||
|| To follow this tutorial, | || To follow this tutorial, | ||
* You should be familiar with compiling and running a '''Rust''' program | * You should be familiar with compiling and running a '''Rust''' program | ||
| − | |||
| − | |||
| − | |||
| + | |- | ||
| + | || '''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 | |
| − | + | ||
|- | |- | ||
| − | || | + | || '''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. | * In Rust, we create variables using the '''let''' statement. | ||
* By default, variables are immutable,meaning their values cannot be changed | * By default, variables are immutable,meaning their values cannot be changed | ||
| Line 73: | Line 65: | ||
We can see a terminal window at the bottom. | We can see a terminal window at the bottom. | ||
|- | |- | ||
| − | || Type at the prompt: | + | || Type at the prompt: |
'''cargo new variables''' | '''cargo new variables''' | ||
| Line 96: | Line 88: | ||
fn main(){ | fn main(){ | ||
| + | |||
let a = 1; | let a = 1; | ||
| + | |||
println!(“The value of a is {}”,a); | println!(“The value of a is {}”,a); | ||
| + | |||
a=2; | a=2; | ||
| + | |||
println!(“The value of a is {}”,a); | println!(“The value of a is {}”,a); | ||
| + | |||
} | } | ||
|| '''let''' keyword is used to declare variables in '''Rus'''t. | || '''let''' keyword is used to declare variables in '''Rus'''t. | ||
| Line 112: | Line 109: | ||
|- | |- | ||
|| | || | ||
| − | || In the menu bar, click on | + | || In the menu bar, click on '''Terminal''' and select '''New Terminal.''' |
|- | |- | ||
|| Type Cargo build | || Type Cargo build | ||
| − | || In the terminal, type '''cargo build '''to compile the Cargo project. | + | || In the terminal, type '''cargo build '''to compile the '''Cargo''' project. |
|- | |- | ||
| Line 142: | Line 139: | ||
|| We can see the output. | || We can see the output. | ||
| − | Both the '''initialized '''and | + | Both the '''initialized '''and modified values are printed successfully. |
|- | |- | ||
|| | || | ||
| Line 148: | Line 145: | ||
|- | |- | ||
|| '''Shadowing''' | || '''Shadowing''' | ||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
|| 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. | ||
Rust allows variable shadowing. | Rust allows variable shadowing. | ||
| + | |||
Here the variable '''x''' is 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. | 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. | We can assign a new value to the new variable while the old variable remains unchanged. | ||
| + | |||
Save the file. | Save the file. | ||
|- | |- | ||
| − | || | + | || type '''cargo run''' |
|| In the terminal, type '''cargo run''' | || In the terminal, type '''cargo run''' | ||
| Line 204: | Line 197: | ||
|| Clear the code window. | || Clear the code window. | ||
| − | Copy and paste the code from the | + | Copy and paste the code from the code file. |
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 local. | + | Curly braces { } define the block scope where the variable access becomes restricted to the local. |
Save the program. | Save the program. | ||
|- | |- | ||
| − | || | + | || Type cargo run |
|| Let us check the output. Run the program. | || Let us check the output. Run the program. | ||
| Line 233: | Line 226: | ||
|- | |- | ||
| − | || | + | || Slide: |
| − | + | ||
| − | + | ||
| − | + | ||
| + | '''Summary''' | ||
| + | || This brings us to the end of this tutorial. | ||
| − | + | Let us summarize. | |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
|- | |- | ||
| − | || | + | || Slide: |
| − | + | Assignment | |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
fn main() { | 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. | ||
| Line 369: | Line 254: | ||
|- | |- | ||
| − | || | + | || Thank You |
| − | + | We would like to thank '''Vishal Pokuri from VIT Vellore '''for content contribution. | |
| − | + | || | |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | | | + | |
| − | | | + | |
| − | + | ||
Thank you for joining. | Thank you for joining. | ||
|- | |- | ||
|} | |} | ||
Latest revision as of 18:34, 4 August 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 modified values are printed successfully. |
| Next let us see the shadowing of variables. | |
| Shadowing | 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. |
| type cargo run | 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 code file. 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 the local. Save the program. |
| Type cargo run | 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. |