Difference between revisions of "Rust-Programming-Language/C2/Variables-and-Mutability/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
Line 5: Line 5:
 
|| '''Narration'''
 
|| '''Narration'''
 
|-
 
|-
|| <div style="color:#000000;">Show Slide: </div>
+
|| '''Show Slide 1''':  
  
<div style="color:#000000;">'''Title Slide'''</div>
+
'''Title Slide'''
|| <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>
+
|| Welcome to the Spoken Tutorial on '''Variables '''and''' Mutability '''in '''Rust.'''
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
|| '''Slide 2'''
+
|| '''Show Slide 2''':
  
 
'''Learning Objective'''
 
'''Learning Objective'''
 
|| In this tutorial, we will learn about:
 
|| In this tutorial, we will learn about:
* <div style="color:#000000;margin-left:1.27cm;margin-right:0cm;">Variables </div>
+
* Variables  
* <div style="color:#000000;margin-left:1.27cm;margin-right:0cm;">Mutability and</div>
+
* Mutability and
* <div style="color:#000000;margin-left:1.27cm;margin-right:0cm;">Data types</div>
+
* Data types
 +
 
 +
|-  
 +
||  '''Show Slide 3''':
  
|- 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'''
|| This tutorial is recorded using</span>
+
|| This tutorial is recorded using
* <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>
+
* '''Ubuntu''' '''Linux OS version 22.04'''
* <div style="margin-left:1.27cm;margin-right:0cm;"><span style="color:#000000;">'''Rust 1.</span>80<span style="color:#000000;">.0 '''</span></div>
+
* '''Rust 1.80.0 '''
* <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>
+
* '''Visual Studio Code 1.19.0'''
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.189cm;padding-right:0.191cm;"
+
|-  
|| '''Slide 4'''
+
|| '''Show Slide 4''':
 +
 
  
 
'''Prerequisites'''
 
'''Prerequisites'''
Line 33: Line 35:
 
'''https://spoken-tutorial.org'''
 
'''https://spoken-tutorial.org'''
 
|| To follow this tutorial,
 
|| To follow this tutorial,
* <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>
+
* You should be familiar with compiling and running a '''Rust''' program
* <div style="margin-left:1.27cm;margin-right:0cm;"><span style="color:#000000;">If </span>not, please go through<span style="color:#000000;"> the </span>prerequisite<span style="color:#000000;"> </span><span style="color:#000000;">'''Rust'''</span><span style="color:#000000;"> tutorial on this website.</span></div>
+
* If not, please go through the prerequisite '''Rust''' tutorial on this website.
 +
 
 +
|-
 +
||  '''Show Slide 5''':
  
<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'''
 
||  
 
||  
* <div style="margin-left:1.27cm;margin-right:0cm;">The files used in this tutorial are provided in the '''Code files''' link of this tutorial page</div>
+
* The files used in this tutorial are provided in the '''Code files''' link of this tutorial page
* <div style="margin-left:1.27cm;margin-right:0cm;">Please download and extract the files.</div>
+
* Please download and extract the files.
* <div style="margin-left:1.27cm;margin-right:0cm;">Make a copy and then use them while practicing.</div>
+
* Make a copy and then use them while practicing.
 +
 
 +
|-
 +
||  '''Show Slide 6''':
  
|- 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
 
|
 
|
* <div style="margin-left:1.27cm;margin-right:0cm;">In Rust, we create variables using the '''let''' statement.</div>
+
* In Rust, we create variables using the '''let''' statement.
* <div style="margin-left:1.27cm;margin-right:0cm;">By default, variables are immutable,meaning their values cannot be changed</div>
+
* By default, variables are immutable,meaning their values cannot be changed
* <div style="margin-left:1.27cm;margin-right:0cm;">You can make them mutable by using '''mut'''.</div>
+
* You can make them mutable by using '''mut'''.
  
|- 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
  
<div style="color:#000000;">Ctrl+Tilde (~)</div>
+
Ctrl+Tilde (~)
  
 
Click on''' Terminal''' and select''' New Terminal.'''
 
Click on''' Terminal''' and select''' New Terminal.'''
Line 69: Line 72:
  
 
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 80: Line 79:
 
|| Go to our working directory '''MyRustProject''' as explained earlier.
 
|| Go to our working directory '''MyRustProject''' as explained earlier.
  
<div style="color:#000000;">Please refer to the '''Additional Reading material '''link of this tutorial.</div>
+
Please refer to the '''Additional Reading material '''link of this tutorial.
 
It explains the steps to create and run the project.
 
It explains the steps to create and run the project.
  
Line 86: Line 85:
  
 
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;"
+
|-  
|| <span style="color:#252525;">Point to</span><span style="color:#252525;">''' main.rs file.'''</span>
+
|| Point to''' main.rs file.'''
  
<div style="color:#252525;">'''Press Ctrl + C to copy '''</div>
+
'''Press Ctrl + C to copy '''
  
<div style="color:#252525;">'''Press Ctrl + V to paste'''</div>
+
'''Press Ctrl + V to paste'''
|| <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>
+
|| In the '''main.rs '''file, copy and paste the '''code''' from the codefile.
|- 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>
+
|| Highlight the code according to narration
  
<div style="color:#252525;">fn main(){</div>
+
fn main(){
<div style="color:#252525;">let a = 1;</div>
+
let a = 1;
<div style="color:#252525;">println!(“The value of a is {}”,a);</div>
+
println!(“The value of a is {}”,a);
<div style="color:#252525;">a=2;</div>
+
a=2;
<div style="color:#252525;">println!(“The value of a is {}”,a);</div>
+
println!(“The value of a is {}”,a);
<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>
+
|| '''let''' keyword is used to declare variables in '''Rus'''t.
  
<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>
+
Here we have initialized the variable '''a''' and assigned the value 1 to it.
  
<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>
+
We are trying to reassign the value 2 to variable '''a.'''
  
<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>
+
After reassigning we are printing the variable’s value to see what the value contains.
  
<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>
+
Press '''Ctrl '''and''' S '''to save the file.
|- 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;"
+
|-  
|| <span style="color:#252525;">Type</span><span style="color:#252525;"> </span><span style="color:#252525;">Cargo build</span>
+
|| Type Cargo build
|| <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>
+
|| In the terminal, type '''cargo build '''to compile the Cargo project.
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
| style="color:#252525;" | Highlight the error
+
|| Highlight the error
|| <div style="color:#252525;">Here, we can see an error - '''cannot assign twice to immutable variable ‘a’.'''</div>
+
|| Here, we can see an error - '''cannot assign twice to immutable variable ‘a’.'''
  
<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>
+
Note that we had mentioned '''variables '''are '''immutable '''by default.
|- 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;"
+
|-  
|| <div style="color:#252525;">Type mut</div>
+
|| Type mut
  
<div style="color:#252525;">Let mut a=1;</div>
+
Let mut a=1;
|| <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>
+
|| Now, type '''mut''' in between '''let''' and '''a.'''
  
<div style="color:#252525;">Here we are telling the compiler that the initialized variable is mutable.</div>
+
Here we are telling the compiler that the initialized variable is mutable.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
| style="color:#252525;" | Type cargo run
+
|| Type cargo run
 
|| Save the file.
 
|| Save the file.
  
In the terminal, type<span style="color:#252525;"> </span><span style="color:#252525;">'''cargo run'''</span>
+
In the terminal, type '''cargo run'''
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
| style="color:#252525;" | Highlight the output
+
|| Highlight the output
|| <div style="color:#252525;">We can see the output.</div>
+
|| We can see the output.
  
<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>
+
Both the '''initialized '''and the modified values are printed successfully.
|- 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 165: Line 164:
 
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.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
||  
 
||  
|| In the terminal, type<span style="color:#252525;"> </span><span style="color:#252525;">'''cargo run'''</span>
+
|| 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.
|- 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 213: Line 212:
 
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>
+
|| Ctrl + S
 +
|| Let us check the output. Run the program.
  
<div style="color:#000000;"></div>
+
We can see a compilation error.
|| <div style="color:#000000;">Let us check the output. Run the program.</div>
+
  
<div style="color:#000000;">We can see a compilation error.</div>
+
Here we tried to print the '''inner_var '''outside of the inner code block.
  
<div style="color:#000000;">Here we tried to print the '''inner_var '''outside of the inner code block.</div>
+
So the program will give an error.
  
<div style="color:#000000;">So the program will give an error.</div>
+
Let us comment the print statement of '''inner_var''' in the outer block.
  
<div style="color:#000000;">Let us comment the print statement of '''inner_var''' in the outer block.</div>
+
Save the program.
  
<div style="color:#000000;">Save the program.</div>
+
Run the program again to see the output.
  
<div style="color:#000000;">Run the program again to see the output.</div>
+
We can see the output displayed for '''outer_var '''as 100 and '''inner_var''' as 200.
  
<div style="color:#000000;">We can see the output displayed for '''outer_var '''as 100 and '''inner_var''' as 200.</div>
+
This shows that any variable outside the braces will have global access.
  
<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;"
+
 
||  
 
||  
 
|| Next we will see about various data types in '''Rust'''.
 
|| Next we will see about various data types in '''Rust'''.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
|| '''Slide:'''
+
|| '''Show Slide 7''':
 +
 
  
 
'''Rust - Data types '''
 
'''Rust - Data types '''
 
||
 
||
* <div style="margin-left:1.27cm;margin-right:0cm;"><span style="color:#252525;">Every value in </span><span style="color:#252525;">'''Rust '''</span><span style="color:#252525;">is of a certain </span><span style="color:#252525;">'''Type.'''</span></div>
+
* Every value in '''Rust '''is of a certain '''Type.'''
* <div style="color:#252525;margin-left:1.27cm;margin-right:0cm;">Rust is a statically typed language that checks the types of variables at compile time.</div>
+
* Rust is a statically typed language that checks the types of variables at compile time.
* <div style="color:#252525;margin-left:1.27cm;margin-right:0cm;">Compiler must know the type of all variables at compile time</div>
+
* Compiler must know the type of all variables at compile time
* <div style="margin-left:1.27cm;margin-right:0cm;"><span style="color:#252525;">This helps the </span><span style="color:#252525;">'''compiler '''</span><span style="color:#252525;">to optimize for </span><span style="color:#252525;">efficient and faster </span><span style="color:#252525;">'''runtime execution.'''</span></div>
+
* This helps the '''compiler '''to optimize for efficient and faster '''runtime execution.'''
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
|| <div style="color:#252525;">'''Slide:'''</div>
+
||'''Show Slide 8''':
  
<div style="color:#252525;">'''Scalar Type'''</div>
+
'''Scalar Type'''
 
|| Data types are divided as
 
|| Data types are divided as
* <div style="margin-left:1.27cm;margin-right:0cm;">Scalar and Compound Types</div>
+
* Scalar and Compound Types
  
A Scalar Type is referred to as a single value. Rust has four Scalar Data types. They are
+
A Scalar Type is referred to as a single value. Rust has four Scalar Data types.  
* <div style="margin-left:2.54cm;margin-right:0cm;">Integers</div>
+
They are:
* <div style="margin-left:2.54cm;margin-right:0cm;">Float</div>
+
* Integers
* <div style="margin-left:2.54cm;margin-right:0cm;">Boolean</div>
+
* Float
* <div style="margin-left:2.54cm;margin-right:0cm;">Character</div>
+
* Boolean
 +
* Character
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
||  
 
||  
 
|| Switch back to the visual code editor.
 
|| Switch back to the visual code editor.
 
|-
 
|-
| style="border-top:1pt solid #000000;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;" |  
+
||  
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;" | Let us see a rust program for various data types.
+
|| Let us see a rust program for various data types.
  
 
Copy paste the code from the code file.
 
Copy paste the code from the code file.
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;" | <div style="color:#000000;">Highlight according to narration</div>
+
|| Highlight according to narration
  
<div style="color:#000000;">fn main() {</div>
+
fn main() {
<div style="color:#000000;">let x: i32 = 100; </div>
+
let x: i32 = 100;  
<div style="color:#000000;">let y: u32 = 200;</div>
+
let y: u32 = 200;
<div style="color:#000000;">let f: f64 = 3.14;</div>
+
let f: f64 = 3.14;
<div style="color:#000000;">let flag: bool = true;</div>
+
let flag: bool = true;
<div style="color:#000000;">let character: char = 'R';</div>
+
let character: char = 'R';
<div style="color:#000000;">println!("Integer: {}", x);</div>
+
println!("Integer: {}", x);
<div style="color:#000000;">println!("Integer: {}", y);</div>
+
println!("Integer: {}", y);
<div style="color:#000000;">println!("Float: {}", f);</div>
+
println!("Float: {}", f);
<div style="color:#000000;">println!("Boolean: {}", flag);</div>
+
println!("Boolean: {}", flag);
<div style="color:#000000;">println!("Character: {}", character);</div>
+
println!("Character: {}", character);
  
<div style="color:#000000;">}</div>
+
}
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;" | <div style="color:#000000;">We use integer data types to store whole numbers.</div>
+
|| We use integer data types to store whole numbers.
  
<div style="color:#000000;">i''' '''specifies signed integer type i.e(it can store both positive or negative value)</div>
+
i''' '''specifies signed integer type i.e(it can store both positive or negative value)
  
<div style="color:#000000;">'''32 '''is the size of the data type i.e (it takes 32 bits of space in memory)</div>
+
'''32 '''is the size of the data type i.e (it takes 32 bits of space in memory)
  
<div style="color:#000000;">Integers can be of many types as i8, i16, i32, i64.</div>
+
Integers can be of many types as i8, i16, i32, i64.
  
<div style="color:#000000;">u specifies the unsigned integer type i.e (it can only store positive integer values.)</div>
+
u specifies the unsigned integer type i.e (it can only store positive integer values.)
  
<div style="color:#000000;">If we try to store negative numbers to u32 type variables, we will get an error.</div>
+
If we try to store negative numbers to u32 type variables, we will get an error.
  
<div style="color:#000000;">Here, the '''f '''character represents a floating point number.</div>
+
Here, the '''f '''character represents a floating point number.
  
<div style="color:#000000;">32 and 64 represent the size in bits.</div>
+
32 and 64 represent the size in bits.
  
<div style="color:#000000;">A boolean data type can have two possible values: true or false.</div>
+
A boolean data type can have two possible values: true or false.
  
<div style="color:#000000;">'''char''' represents the character type variable and we use single quotes to represent a character.</div>
+
'''char''' represents the character type variable and we use single quotes to represent a character.
  
<div style="color:#000000;">We can also store special characters like $, & etc. using the character type.</div>
+
We can also store special characters like $, & etc. using the character type.
  
<div style="color:#000000;">Save the program.</div>
+
Save the program.
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;" |  
+
||  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;" | Let us compile and run the program and see the output.
+
| Let us compile and run the program and see the output.
|- 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 '''cargo run.'''
  
 
We can see the various data types printed as output.
 
We can see the various data types printed as output.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
|| '''Slide:'''
+
|| '''Show Slide 9''':
  
 
'''Type Inference in Rust'''
 
'''Type Inference in Rust'''
 
||  
 
||  
* <div style="margin-left:1.27cm;margin-right:0cm;">In Rust we can create variables without mentioning a data type</div>
+
* In Rust we can create variables without mentioning a data type
* <div style="margin-left:1.27cm;margin-right:0cm;">let x=50;</div>
+
* let x=50;
* <div style="margin-left:1.27cm;margin-right:0cm;">Rust will automatically set i32 as default type for integer by looking at the value 50.</div>
+
* Rust will automatically set i32 as default type for integer by looking at the value 50.
* <div style="margin-left:1.27cm;margin-right:0cm;">This process is Type Inference.</div>
+
* This process is Type Inference.
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
|| '''Slide:'''
+
|| '''Show Slide 10''':
  
 
'''Constants'''
 
'''Constants'''
<div style="color:#171717;">const PI: f32 = 3.14159265358979323846; </div>
+
const PI: f32 = 3.14159265358979323846;  
<div style="color:#000000;">fn main() { </div>
+
fn main() {  
<div style="color:#000000;">let radius = 5.0; </div>
+
let radius = 5.0;  
<div style="color:#000000;">let circumference = 2.0 * PI * radius; </div>
+
let circumference = 2.0 * PI * radius;  
<div style="color:#000000;">println!("The circumference of a circle with radius {} is {}", radius, circumference); </div>
+
println!("The circumference of a circle with radius {} is {}", radius, circumference);  
<div style="color:#000000;">} </div>
+
}  
|| <div style="color:#171717;">This is an example for constants.</div>
+
|| This is an example for constants.
* <div style="color:#171717;margin-left:1.27cm;margin-right:0cm;">Constants are variables that are immutable and have a fixed value.</div>
+
* Constants are variables that are immutable and have a fixed value.
* <div style="margin-left:1.27cm;margin-right:0cm;"><span style="color:#171717;">They are declared using the </span><span style="color:#171717;">'''const'''</span><span style="color:#171717;"> </span><span style="color:#171717;">keyword. </span></div>
+
* They are declared using the '''const''' keyword.  
* <div style="color:#171717;margin-left:1.27cm;margin-right:0cm;">You must declare the type of the value.</div>
+
* You must declare the type of the value.
 
+
|-  
<div style="color:#171717;"></div>
+
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
 
||  
 
||  
 
|| 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:
+
|| '''Show Slide 11''':
  
Summary
+
'''Summary'''
 
|| In this tutorial, we learnt about
 
|| In this tutorial, we learnt about
* <div style="margin-left:1.27cm;margin-right:0cm;">Variables </div>
+
* Variables  
* <div style="margin-left:1.27cm;margin-right:0cm;">Mutability and</div>
+
* Mutability and
* <div style="margin-left:1.27cm;margin-right:0cm;">Data types</div>
+
* Data types
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
|| Slide:
+
|| '''Show Slide 12''':
  
Assignment
+
'''Assignment '''
  
 
fn main() {
 
fn main() {
Line 368: Line 365:
 
|| As an assignment, do the following.
 
|| As an assignment, do the following.
  
* <div style="margin-left:1.27cm;margin-right:0cm;">Run the above program</div>
+
* Run the above program
* <div style="margin-left:1.27cm;margin-right:0cm;">Analyse the error and correct the program.</div>
+
* Analyse the error and correct the program.
  
 
|-
 
|-
| style="border:1pt solid #252525;padding-top:0.176cm;padding-bottom:0.176cm;padding-left:0.159cm;padding-right:0.176cm;" | Show Slide:
+
|| '''Show Slide 13''':
  
 
'''About Spoken Tutorial Project'''
 
'''About Spoken Tutorial Project'''
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;" | The video at the following link summarizes the Spoken Tutorial project.
+
|| The video at the following link summarizes the Spoken Tutorial project.
  
 
Please download and watch it.
 
Please download and watch it.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
|| Show Slide:
+
|| '''Show Slide 14''':
  
 
'''Spoken Tutorial Workshops'''
 
'''Spoken Tutorial Workshops'''
Line 385: Line 382:
  
 
For more details, please write to us.
 
For more details, please write to us.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Slide : Forum for specific questions
 
|| Slide : Forum for specific questions
 
|| Please post your timed queries in this forum
 
|| Please post your timed queries in this forum
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
|| Acknowledgement
+
|| '''Show Slide 15''':
 
|| Spoken Tutorial project was established by the Ministry of Education(MoE), Govt of India
 
|| Spoken Tutorial project was established by the Ministry of Education(MoE), Govt of India
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
|| Acknowledgement
+
|| '''Show Slide 16''':
 +
 
 +
'''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.
 
|-
 
|-
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;" | Thank You
+
|| Thank You
| style="border:1pt solid #252525;padding-top:0.176cm;padding-bottom:0.176cm;padding-left:0.123cm;padding-right:0.176cm;" | This tutorial is contributed by '''Nirmala Venkat''' and '''Ketki Bhamble''' from the spoken tutorial team.
+
|| This tutorial is contributed by '''Nirmala Venkat''' and '''Ketki Bhamble''' from the spoken tutorial team.
  
 
Thank you for joining.
 
Thank you for joining.
 
|-
 
|-
 
|}
 
|}
<div style="margin-left:-1.588cm;margin-right:-1.429cm;"></div>
 

Revision as of 21:28, 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:
  • Variables
  • Mutability and
  • Data types
Show Slide 3:


System Requirements

This tutorial is recorded using
  • Ubuntu Linux OS version 22.04
  • Rust 1.80.0
  • Visual Studio Code 1.19.0
Show Slide 4:


Prerequisites

https://spoken-tutorial.org

To follow this tutorial,
  • You should be familiar with compiling and running a Rust program
  • If not, please go through the prerequisite Rust tutorial on this website.
Show Slide 5:


Code Files

  • The files used in this tutorial are provided in the Code files link of this tutorial page
  • Please download and extract the files.
  • Make a copy and then use them while practicing.
Show Slide 6:

Variables let a = 10; //immutable let mut b=15;//mutable

  • In Rust, we create variables using the let statement.
  • By default, variables are immutable,meaning their values cannot be changed
  • You can make them mutable by using mut.
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.

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:


Rust - Data types

  • Every value in Rust is of a certain Type.
  • Rust is a statically typed language that checks the types of variables at compile time.
  • Compiler must know the type of all variables at compile time
  • This helps the compiler to optimize for efficient and faster runtime execution.
Show Slide 8:

Scalar Type

Data types are divided as
  • Scalar and Compound Types

A Scalar Type is referred to as a single value. Rust has four Scalar Data types. They are:

  • Integers
  • Float
  • Boolean
  • Character
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 i.e(it can store both positive or negative value)

32 is the size of the data type i.e (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 i.e (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 $, & etc. 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

  • In Rust we can create variables without mentioning a data type
  • let x=50;
  • Rust will automatically set i32 as default type for integer by looking at the value 50.
  • This process is Type Inference.
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.
  • Constants are variables that are immutable and have a fixed value.
  • They are declared using the const keyword.
  • You must declare the type of the value.
This brings us to the end of this tutorial.

Let us summarize.

Show Slide 11:

Summary

In this tutorial, we learnt about
  • Variables
  • Mutability and
  • Data types
Show Slide 12:

Assignment

fn main() { let y: u32 = - 200; println!("Integer: {}", y); }

As an assignment, do the following.
  • Run the above program
  • Analyse the error and correct the program.
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.

Slide : Forum for specific questions Please post your timed queries in this forum
Show Slide 15: Spoken Tutorial project was established by the Ministry of Education(MoE), Govt of India
Show Slide 16:

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.

Contributors and Content Editors

Madhurig, Nirmala Venkat