Difference between revisions of "Rust-Programming-Language/C2/Variables-and-Mutability/English"
| Line 44: | Line 44: | ||
'''Code Files''' | '''Code Files''' | ||
|| | || | ||
| − | * The files used in this tutorial are provided in the '''Code files''' link of this tutorial page | + | * The files used in this tutorial are provided in the '''Code files''' link of this tutorial page. |
* Please download and extract the files. | * Please download and extract the files. | ||
* Make a copy and then use them while practicing. | * Make a copy and then use them while practicing. | ||
| Line 54: | Line 54: | ||
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 |
* You can make them mutable by using '''mut'''. | * You can make them mutable by using '''mut'''. | ||
| Line 69: | Line 69: | ||
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. | ||
| Line 79: | Line 79: | ||
|| 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. | + | Please refer to the '''Additional Reading material ''' link of this tutorial to create and run the project. |
| + | |||
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. | ||
| Line 90: | Line 91: | ||
'''Press Ctrl + C to copy ''' | '''Press Ctrl + C to copy ''' | ||
| − | '''Press Ctrl + V to paste''' | + | '''Press Ctrl + V to paste''' |
| − | || In the '''main.rs '''file, copy and paste the '''code''' from the codefile. | + | |
| + | || In the '''main.rs ''' file, copy and paste the '''code''' from the codefile. | ||
|- | |- | ||
|| Highlight the code according to narration | || Highlight the code according to narration | ||
| Line 101: | Line 103: | ||
println!(“The value of a is {}”,a); | println!(“The value of a is {}”,a); | ||
} | } | ||
| − | || '''let''' keyword is used to declare variables in ''' | + | || '''let''' keyword is used to declare variables in '''Rust'''. |
Here we have initialized the variable '''a''' and assigned the value 1 to it. | Here we have initialized the variable '''a''' and assigned the value 1 to it. | ||
| Line 109: | Line 111: | ||
After reassigning we are printing the variable’s value to see what the value contains. | After reassigning we are printing the variable’s value to see what the value contains. | ||
| − | Press '''Ctrl '''and''' S '''to save the file. | + | Press '''Ctrl ''' and ''' S '''to save the file. |
|- | |- | ||
| − | || | + | || In the menu bar, 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.''' | ||
|- | |- | ||
| − | || Type | + | || 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 122: | Line 124: | ||
|| Here, we can see an error - '''cannot assign twice to immutable variable ‘a’.''' | || Here, we can see an error - '''cannot assign twice to immutable variable ‘a’.''' | ||
| − | Note that we had mentioned '''variables '''are '''immutable '''by default. | + | Note that we had mentioned '''variables ''' are '''immutable ''' by default. |
|- | |- | ||
|| | || | ||
| Line 137: | Line 139: | ||
|| Save the file. | || Save the file. | ||
| − | In the terminal, type '''cargo run''' | + | In the terminal, type '''cargo run'''. |
|- | |- | ||
|| Highlight the output | || Highlight the output | ||
|| We can see the output. | || We can see the output. | ||
| − | Both the '''initialized '''and the modified values are printed successfully. | + | Both the '''initialized ''' and the modified values are printed successfully. |
|- | |- | ||
|| | || | ||
| Line 156: | Line 158: | ||
'''println!("x is {}", x);''' | '''println!("x is {}", x);''' | ||
'''}''' | '''}''' | ||
| + | |||
| + | Press Ctrl + S to save the file. | ||
|| 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'''. |
Check the output. | Check the output. | ||
| − | It prints the value of x as 5 and 7 as the result of shadowing. | + | It prints the value of '''x''' as 5 and 7 as the result of shadowing. |
|- | |- | ||
|| | || | ||
| Line 204: | Line 212: | ||
|| 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. | ||
| Line 218: | Line 226: | ||
We can see a compilation error. | We can see a compilation error. | ||
| − | Here we tried to print the '''inner_var '''outside of the inner code block. | + | Here we tried to print the '''inner_var ''' outside of the inner code block. |
So the program will give an error. | So the program will give an error. | ||
| Line 228: | Line 236: | ||
Run the program again to see the output. | Run the program again to see the output. | ||
| − | We can see the output displayed for '''outer_var '''as 100 and '''inner_var''' as 200. | + | 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. | This shows that any variable outside the braces will have global access. | ||
| Line 286: | Line 294: | ||
|| We use integer data types to store whole numbers. | || We use integer data types to store whole numbers. | ||
| − | + | '''i''' specifies signed integer type that is, it can store both positive or negative value. | |
| − | '''32 '''is the size of the data type | + | '''32 '''is the size of the data type that is, it takes 32 bits of space in memory. |
Integers can be of many types as i8, i16, i32, i64. | Integers can be of many types as i8, i16, i32, i64. | ||
| − | u specifies the unsigned integer type | + | '''u''' specifies the unsigned integer type that is, it can only store positive integer values. |
| − | If we try to store negative numbers to u32 type variables, we will get an error. | + | If we try to store negative numbers to '''u32''' type variables, we will get an error. |
| − | Here, the '''f '''character represents a floating point number. | + | Here, the '''f ''' character represents a floating point number. |
32 and 64 represent the size in bits. | 32 and 64 represent the size in bits. | ||
| − | A boolean data type can have two possible values: true or false. | + | A '''boolean''' data type can have two possible values: true or false. |
'''char''' represents the character type variable and we use single quotes to represent a character. | '''char''' represents the character type variable and we use single quotes to represent a character. | ||
| − | We can also store special characters like $ | + | We can also store special characters like '''$''' and '''&''' using the character type. |
Save the program. | Save the program. | ||
| Line 323: | Line 331: | ||
* let x=50; | * let x=50; | ||
* Rust will automatically set i32 as default type for integer by looking at the value 50. | * Rust will automatically set i32 as default type for integer by looking at the value 50. | ||
| − | * This process is Type Inference. | + | * This process is '''Type Inference'''. |
|- | |- | ||
| Line 383: | Line 391: | ||
For more details, please write to us. | For more details, please write to us. | ||
|- | |- | ||
| − | || Slide : Forum for specific questions | + | || '''Show Slide 15''': |
| + | ''' | ||
| + | Forum for specific questions''' | ||
|| Please post your timed queries in this forum | || Please post your timed queries in this forum | ||
| − | |||
| − | |||
| − | |||
|- | |- | ||
|| '''Show Slide 16''': | || '''Show Slide 16''': | ||
| + | |||
| + | '''Acknowlegdements''' | ||
| + | |||
| + | || Spoken Tutorial project was established by the Ministry of Education(MoE), Govt of India. | ||
| + | |- | ||
| + | || '''Show Slide 17''': | ||
'''Thank You''' | '''Thank You''' | ||
| − | || We would like to thank '''Vishal Pokuri from VIT Vellore '''for content contribution. | + | || We would like to thank '''Vishal Pokuri from VIT Vellore ''' for content contribution. |
|- | |- | ||
|| Thank You | || Thank You | ||
Revision as of 22:00, 5 May 2025
| Visual Cue | Narration |
| Show Slide 1:
Title Slide |
Welcome to the Spoken Tutorial on Variables and Mutability in Rust. |
| Show Slide 2:
Learning Objective |
In this tutorial, we will learn about:
|
| Show Slide 3:
|
This tutorial is recorded using
|
| Show Slide 4:
|
To follow this tutorial,
|
| Show Slide 5:
|
|
| Show 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 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 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. | 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); } Press Ctrl + S to save the file. |
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. |
| Ctrl + S | 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. |
| Next we will see about various data types in Rust. | |
| Show Slide 7:
|
|
| Show Slide 8:
Scalar Type |
Data types are divided as
A Scalar Type is referred to as a single value. Rust has four Scalar Data types. They are:
|
| Switch back to the visual code editor. | |
| Let us see a rust program for various data types.
Copy paste the code from the code file. | |
| Highlight according to narration
fn main() { let x: i32 = 100; let y: u32 = 200; let f: f64 = 3.14; let flag: bool = true; let character: char = 'R'; println!("Integer: {}", x); println!("Integer: {}", y); println!("Float: {}", f); println!("Boolean: {}", flag); println!("Character: {}", character); } |
We use integer data types to store whole numbers.
i specifies signed integer type that is, it can store both positive or negative value. 32 is the size of the data type that is, it takes 32 bits of space in memory. Integers can be of many types as i8, i16, i32, i64. u specifies the unsigned integer type that is, it can only store positive integer values. If we try to store negative numbers to u32 type variables, we will get an error. Here, the f character represents a floating point number. 32 and 64 represent the size in bits. A boolean data type can have two possible values: true or false. char represents the character type variable and we use single quotes to represent a character. We can also store special characters like $ and & using the character type. Save the program. |
| Let us compile and run the program and see the output. | |
| In the terminal, type cargo run.
We can see the various data types printed as output. | |
| Show Slide 9:
Type Inference in Rust |
|
| Show Slide 10:
Constants const PI: f32 = 3.14159265358979323846; fn main() { let radius = 5.0; let circumference = 2.0 * PI * radius; println!("The circumference of a circle with radius {} is {}", radius, circumference); } |
This is an example for constants.
|
| This brings us to the end of this tutorial.
Let us summarize. | |
| Show Slide 11:
Summary |
In this tutorial, we learnt about
|
| Show Slide 12:
Assignment fn main() { let y: u32 = - 200; println!("Integer: {}", y); } |
As an assignment, do the following.
|
| Show Slide 13:
About Spoken Tutorial Project |
The video at the following link summarizes the Spoken Tutorial project.
Please download and watch it. |
| Show Slide 14:
Spoken Tutorial Workshops |
The Spoken Tutorial Project Team conducts workshops and gives certificates.
For more details, please write to us. |
| Show Slide 15:
Forum for specific questions |
Please post your timed queries in this forum |
| Show Slide 16:
Acknowlegdements |
Spoken Tutorial project was established by the Ministry of Education(MoE), Govt of India. |
| Show Slide 17:
Thank You |
We would like to thank Vishal Pokuri from VIT Vellore for content contribution. |
| Thank You | This tutorial is contributed by Nirmala Venkat and Ketki Bhamble from the spoken tutorial team.
Thank you for joining. |