Rust/C2/Control-Flow-in-Rust/English
Title of the script: Control-Flow
Author: Jayesh Katta Ramalingaiah
Domain Reviewer:
Novice Reviewer:
Keywords: Rust, variables, if, else, else if, conditions
|
|
Slide: Title | Welcome to the spoken tutorial on “Control-Flow in Rust”. |
Slide:
Learning Objectives |
In this tutorial, we will learn about the:
|
Slide: System Specifications | This tutorial is recorded using:
However you may use any other editor of your choice. |
Slide : Pre-requisites | To practice this tutorial,
|
Slide: Code files |
|
Slide: Control Flow |
|
Slide: Control Flow
[Highlight]: if expressions |
|
Slide: Control Flow
[Highlight]: else if |
|
Slide: Control Flow -1
|
|
Slide: Control Flow -2 |
Let us understand all this by using an example. |
Press Ctrl+Alt+T keys | Open the terminal by pressing Ctrl,Alt and T keys simultaneously on the keyboard.
|
Only Narration | Here onwards, please remember to press the Enter key after typing each command. |
[Terminal] Type:
[Enter] |
Using cd command go to the Rust practice folder which we created earlier. |
[Terminal] Type:
[Enter] |
Let us create a new project named controlFlow.
|
Open Visual Studio Code editor. | You may use any editor of your choice.
|
[Editor]
Open Folder -> control_flow |
Open the created project by clicking on the Open folder link in the Welcome page.
|
[Editor]
Click on control_flow |
Under the EXPLORER section, expand the project folder “controlFlow” by clicking on it. |
[Editor] Expand src and click on main.rs | Then expand src and open the main.rs file. |
[Editor] Type:
let a = 30; let b = 20; if a>b{ println!(“A is greater than B”); } else { println!(“B is greater than A”); } } |
In the editor, replace the code as shown. |
[Editor] Highlight:
|
Here we are checking whether the condition a is greater than b or not. |
[Editor] Highlight:
|
When the condition is satisfied, the print statement inside the if block gets printed. |
[Editor] Highlight:
println!(“B is greater than A”); } |
If not, the print statement in the else block gets executed. |
Ctrl + S | Save the file. |
Switch to terminal | Switch to the terminal. |
[Terminal] Type:
|
Go to the project folder controlFlow using the cd command. |
[Terminal] Type:
|
Now type cargo run. |
[Terminal] Highlight:
|
The print statement in the if block is executed and printed, as the condition is satisfied. |
Switch to Editor | Switch back to the editor. |
[Editor] Modify:
let marks = 50; if marks>35{ println!(“You are qualified!”); } } |
In the editor, modify the code as shown. |
[Editor] Highlight:
if(marks>35){ println!(“You are qualified!”); } |
Here, in this example, we are considering only the if condition.
|
Ctrl + S | Save the file. |
Switch to terminal | Switch back to the terminal. |
[Terminal] Type:
|
Run the project. |
[Terminal] Highlight:
|
As the condition (marks greater than 35) is satisfied, the block gets executed.
|
Switch to Editor | Switch back to the editor. |
[Editor] Modify:
fn main(){ let a = 5; if a%2 == 0{ println!(“Number is divisible by 2”); } else if a%3 == 0{ println!(“Number is divisible by 3”); } else { println!(“Number is not divisible by 2 and 3”); } } |
In the editor, let’s update the code as shown.
|
[Editor] Highlight:
println!(“Number is divisible by 2”); } |
Here, we are checking a condition if a % 2 == 0 or not.
|
[Editor] Highlight:
println!(“Number is divisible by 3”); } |
else if block checks the condition if a % 3 == 0 or not.
|
[Editor] Highlight:
else { println!(“Number is not divisible by 2 and 3”); } |
When this condition also fails, the else block is executed.
And prints this message as the above two conditions are not satisfied. |
Ctrl + S | Save the file. |
Switch to terminal | Switch back to the terminal. |
[Terminal] Type:
|
Now type cargo run. |
[Terminal] Highlight:
|
Number is not divisible by 2 and 3 is printed because the above two conditions are not satisfied. |
Only narration | That’s how the if-else if-else statements work in Rust. |
Next, let’s learn about loops. | |
Slide: Control Flow - Loops |
|
Slide: Control Flow - Loops |
|
Only Narration | Let us take an example and understand Loops better. |
[Editor] Type:
loop { println!(“Hello World!”); } } |
Go back to the editor and replace the code as shown. |
[Editor] Highlight:
|
loop is a keyword in Rust which iterates a block until it fails. |
Ctrl + S | Save the file. |
Switch to terminal | Switch to the terminal. |
[Terminal] Type:
|
Now type cargo run. |
[Terminal] Show:
(being printed infinite times) |
We can see Hello World being printed infinite times, until the memory in the stack ends.
|
Only Narration | Press Ctrl + C keys to terminate this loop. |
Switch to Editor | Switch back to the editor. |
[Editor] Modify:
println!(“Hello, World!”); break; } |
In the editor, modify the code as shown. |
[Editor] Highlight:
break; |
Here, we are using break statements to break the execution of the loop. |
Ctrl + S | Save the file. |
Switch to terminal | Switch back to the terminal. |
[Terminal] Type:
|
Run the project. |
[Terminal] Highlight:
|
Notice now, we can see Hello, World printed only once.
|
Only Narration | This is the basic way to loop in Rust.
|
Switch to Editor | Switch back to the editor. |
[Editor] Type:
let mut a = 3; while a!=0 { println!(“{}”,a); a = a - 1; } } |
In the editor, replace the code as shown. |
[Editor] Highlight:
let mut a = 3; |
Here we have initialized a mutable variable a and assigned the value 3 to it. |
[Editor] Highlight:
|
After this, the while keyword is used to initialize a while loop.
|
[Editor] Highlight:
a = a - 1; |
Here we are printing the number and then reducing the value by 1. |
Ctrl + S | Save the file. |
Switch to terminal | Switch back to the terminal. |
[Terminal] Type:
|
Run the project and see the output. |
[Terminal] Highlight:
2 1 |
So now, we have successfully executed the while loop.
|
Only Narration | Now let us learn about for loop. |
Switch to Editor | Switch back to the editor. |
[Editor] Type:
let arr = [2,3,5,7,11]; for value in arr.iter() { println!(“Value = {}”,value); } } |
In the editor, replace the code as shown. |
[Editor] Highlight:
|
Here we have initialized an array with few number elements in it. |
[Editor] Highlight:
for value in arr.iter() { |
So here value means each and every number element present in the array.
|
[Video Editing]:
Notify: |
Rust std module provides three forms of iteration.
|
[Editor] Highlight:
|
And, in the block we are printing the value for each and every iteration. |
Ctrl + S | Save the file. |
Switch to terminal | Switch back to the terminal. |
[Terminal] Type:
|
Run the project. |
[Terminal] Highlight:
Value = 3 Value = 5 Value = 7 Value = 11 |
The loop has executed successfully and printed all the values in the array.
|
Only narration | That’s how a for loop works in Rust. |
Only narration | With this we have come to the end of this tutorial.
|
Slide: Summary | In this tutorial, we have learnt:
|
Slide: Assignment | As an assignment,
|
Slide: About Spoken Tutorial Project |
|
Slide: Spoken tutorial workshops |
|
Slide: Forum questions | Pls post your timed queries in this forum |
Slide: Acknowledgement | Spoken Tutorial Project is funded by Ministry of Education, Government of India |
Slide: Thanks | This is Jayesh signing off. Thank you for joining |