Difference between revisions of "Rust-Programming-Language/C2/Control-Flow/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with "{| border="1" |- || '''Visual Cue''' || '''Narration''' |- || '''Slide 1''' || <span style="color:#000000;">Welcome to the Spoken Tutorial on </span><span style="color:#00000...")
 
 
Line 6: Line 6:
  
 
|| '''Slide 1'''
 
|| '''Slide 1'''
|| <span style="color:#000000;">Welcome to the Spoken Tutorial on </span><span style="color:#000000;">'''Control Flow '''</span><span style="color:#000000;">in </span><span style="color:#000000;">'''Rust.'''</span>
+
|| Welcome to the Spoken Tutorial on '''Control Flow '''in '''Rust'''.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
|| <div style="color:#000000;">'''Slide 2'''</div>
+
|| '''Slide 2'''
  
<div style="color:#000000;">'''Learning Objectives'''</div>
+
'''Learning Objectives'''
 
|| In this tutorial, we will learn about
 
|| In this tutorial, we will learn about
* Control Flow Statement </div>
+
* Control Flow Statement  
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.136cm;padding-right:0.191cm;"
+
|-  
|| <span style="color:#000000;">'''Slide </span>3'''
+
|| '''Slide 3'''
  
<div style="color:#000000;">'''System Requirements'''</div>
+
'''System Requirements'''
|| <span style="color:#000000;">This tutorial</span> is recorded<span style="color:#000000;"> using</span>
+
|| This tutorial is recorded using
* <div style="color:#000000;">Ubuntu Linux OS version 22.04</div>
+
* Ubuntu Linux OS version 22.04
* <span style="color:#000000;">Rust 1.</span><span style="color:#000000;">80</span><span style="color:#000000;">.0 </span>
+
* Rust 1.80.0  
* <span style="color:#000000;">Visual </span>S<span style="color:#000000;">tudio </span>C<span style="color:#000000;">ode 1.19.0</span>
+
* Visual Studio Code 1.19.0
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.136cm;padding-right:0.191cm;"
+
|-  
|| <span style="color:#000000;">'''Slide </span>4'''
+
|| '''Slide 4'''
  
<div style="color:#000000;">'''Prerequisites'''</div>
+
'''Prerequisites'''
||  
+
|| To follow this tutorial,
* <span style="color:#000000;">You should be familiar with compiling and running Rust files</span>
+
* You should be familiar with compiling and running a Rust program.
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.136cm;padding-right:0.191cm;"
+
|-  
|| <span style="color:#000000;">'''Slide'''</span> '''5'''
+
|| '''Slide''' '''5'''
  
<div style="color:#000000;">'''Code Files'''</div>
+
'''Code Files'''
 
||
 
||
* <div style="color:#000000;">The following code file is required to practise this tutorial</div>
+
* The following code file is required to practise this tutorial
* <div style="color:#000000;">This file is provided in the code files link of this tutorial page</div>
+
* 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.136cm;padding-right:0.191cm;"
+
|-  
| style="color:#000000;" |  
+
||
|| W<span style="color:#000000;">e will see about conditional statements in Rust.</span>
+
|| We will see about conditional statements in Rust.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.136cm;padding-right:0.191cm;"
+
|-  
|| <span style="color:#000000;">'''Slide</span> 6'''
+
|| '''Slide 6'''
  
<div style="color:#000000;">'''Conditional Statements'''</div>
+
'''Conditional Statements'''
|| <div style="color:#000000;">Rust supports </div>
+
|| Rust supports  
* <div style="color:#000000;">if </div>
+
* if  
* <div style="color:#000000;">else if </div>
+
* else if  
* <span style="color:#000000;">else</span><span style="color:#000000;">''' '''</span>
+
* else  
  
statements <span style="color:#000000;">for making decisions based on conditions.</span>
+
statements for making decisions based on conditions.
  
It <span style="color:#000000;">always returns a boolean value.</span>
+
It always returns a '''boolean''' value.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
|| <span style="color:#000000;">Open </span><span style="color:#000000;">'''Visual'''</span><span style="color:#000000;"> </span><span style="color:#000000;">'''code editor'''</span>
+
|| Open '''Visual''' '''code editor'''
|| <span style="color:#000000;">Let us open the </span><span style="color:#000000;">'''visual code editor'''</span><span style="color:#000000;"> and</span><span style="color:#000000;"> understand the control </span><span style="color:#000000;">statements</span><span style="color:#000000;"> with examples.</span>
+
|| Let us open the '''visual code editor''' and understand the control statements with examples.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
| style="color:#000000;" |  
+
||
|| <span style="color:#000000;">In the menu bar, click on </span><span style="color:#000000;">'''terminal'''</span><span style="color:#000000;"> and select</span><span style="color:#000000;">''' New Terminal'''</span>
+
|| In the menu bar, click on '''Terminal''' and select''' New Terminal'''
  
<div style="color:#000000;">We can see a terminal window at the bottom.</div>
+
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;"
+
|-  
 
|| > cd '''MyRustProject'''
 
|| > cd '''MyRustProject'''
  
Line 71: Line 71:
  
 
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;"
+
|-  
|| <div style="color:#000000;"></div>
+
||
 +
|| In the '''main.rs '''file, copy and paste the code from the code file.
 +
|-
 +
|| fn main() {
  
<div style="color:#000000;"></div>
+
let n = 100;  
|| <span style="background-color:#ffffff;color:#000000;">In the </span><span style="background-color:#ffffff;color:#000000;">'''main.rs '''</span><span style="background-color:#ffffff;color:#000000;">file, </span><span style="background-color:#ffffff;">copy and paste the code from the code file.</span>
+
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|| <div style="color:#000000;">fn main() { </div>
+
  
<div style="color:#000000;">let n = 100; </div>
+
if n>100{
  
<div style="color:#000000;">if n>100{ </div>
+
println!("Greater than 100");
  
<div style="color:#000000;">println!("Greater than 100"); </div>
+
}else if n==100{
  
<div style="color:#000000;">}else if n==100{ </div>
+
println!("Equal to 100");
  
<div style="color:#000000;">println!("Equal to 100"); </div>
+
}else {
  
<div style="color:#000000;">}else { </div>
+
println!("Smaller than 100");
  
<div style="color:#000000;">println!("Smaller than 100"); </div>
+
}
  
<div style="color:#000000;">} </div>
+
}  
 +
|| Let us look at this example for an''' if else''' statement.
  
<div style="color:#000000;">} </div>
+
If n is greater than 100, it prints a “greater than 100” statement.  
|| <span style="color:#000000;">Let us look at this example </span>for an''' if else'''<span style="color:#000000;"> statement.</span>
+
  
<span style="color:#000000;">If n is greater than 100, it </span>prints a<span style="color:#000000;"> </span>“greater than 100”<span style="color:#000000;"> statement. </span>
+
If n equals 100, the second print statement will be executed.  
  
<div style="color:#000000;">If n equals 100, the second print statement will be executed. </div>
+
If n is less than 100, it prints “smaller than 100”.
  
<span style="color:#000000;">If n is less than 100, it prints </span>“smaller than 100”<span style="color:#000000;">.</span>
+
You can include multiple '''else if''' statements to check multiple conditions in sequence.  
  
<span style="color:#000000;">You can include multiple </span><span style="color:#000000;">'''else if'''</span><span style="color:#000000;"> statements </span><span style="background-color:#ffffff;color:#000000;">to check multiple conditions in sequence. </span>
+
The condition must be true to run the code wrapped to it.
  
<div style="color:#000000;">The condition must be true to run the code wrapped to it.</div>
+
Press '''Ctrl and S '''to save the file.
 
+
<span style="color:#000000;">Press </span><span style="color:#000000;">'''ctrl and s '''</span><span style="color:#000000;">to save the file.</span>
+
  
 
Now let us run the program.
 
Now let us run the program.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 +
|| 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.'''
|| <span style="color:#000000;">In the menu bar, click on </span>'''T<span style="color:#000000;">erminal'''</span><span style="color:#000000;"> and select </span><span style="color:#000000;">'''New Terminal.'''</span>
 
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| > cargo run
 
|| > cargo run
|| <span style="color:#000000;">In the terminal</span>,<span style="color:#000000;"> type </span><span style="color:#000000;">'''cargo run '''</span><span style="color:#000000;">to see the output.</span>
+
|| In the terminal, type '''cargo run '''to see the output.
  
The output shows '''“equal to 100” '''as we assigned '''n '''as 100 in the program.
+
The output shows '''“equal to 100” '''as we assigned '''n ''' as 100 in the program.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
| style="color:#000000;" |  
+
||
|| <span style="color:#000000;">Next let us see </span><span style="color:#000000;">'''loop'''</span><span style="color:#000000;"> statements in Rust.</span>
+
|| Next let us see '''loop''' statements in Rust.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Slide
 
|| Slide
  
 
Loop
 
Loop
 
||
 
||
* <div style="margin-left:1.27cm;margin-right:0cm;">A loop is used to repeat a block of code until the specified condition is met</div>
+
* A loop is used to repeat a block of code until the specified condition is met
  
* <div style="margin-left:1.27cm;margin-right:0cm;">The loop keyword creates an infinite loop unless you explicitly break out of it</div>
+
* The loop keyword creates an infinite loop unless you explicitly break out of it
  
* <div style="margin-left:1.27cm;margin-right:0cm;">Rust provides several ways to loop, including '''loop''', '''while''', and '''for'''</div>
+
* Rust provides several ways to loop, including '''loop''', '''while''', and '''for'''
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
||  
 
||  
 
|| Let us see an example for a '''loop''' program.
 
|| Let us see an example for a '''loop''' program.
  
Clear the code window and replace the code from the code file as shown.
+
Clear the code window and replace the code from the Code file as shown.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| fn main() {
 
|| fn main() {
  
Line 163: Line 161:
 
If there is no if statement, it will execute infinitely.
 
If there is no if statement, it will execute infinitely.
  
Press '''ctrl and s '''to save the file.
+
Press '''Ctrl and S '''to save the file.
  
 
Let us run the program.
 
Let us run the program.
|- 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'''
Line 173: Line 171:
  
 
It stops executing when the '''count''' reaches 5.
 
It stops executing when the '''count''' reaches 5.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
||  
 
||  
 
|| Next we will see how the '''while''' loop works.
 
|| Next we will see how the '''while''' loop works.
  
Replace the code from the codefile with the '''while''' loop program.
+
Replace the code from the code file with the '''while''' loop program.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| fn main() {
 
|| fn main() {
  
Line 202: Line 200:
 
After the 5th iteration, the value of the '''counter''' will be 6.
 
After the 5th iteration, the value of the '''counter''' will be 6.
  
So the condition, '''counter''' '''< 6 '''becomes false and the loop is terminated.
+
So the condition, '''counter < 6''' becomes false and the loop is terminated.
  
 
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;"
+
|-  
 
||  
 
||  
 
|| In the terminal, type '''cargo run'''
 
|| In the terminal, type '''cargo run'''
  
 
Check the output.
 
Check the output.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
||  
 
||  
|| <span style="background-color:#ffffff;">Next we will see about </span><span style="background-color:#ffffff;">'''For'''</span><span style="background-color:#ffffff;"> loop.</span>
+
|| Next we will see about '''For''' loop.
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| '''Slide 7'''
 
|| '''Slide 7'''
  
Line 223: Line 221:
 
* In a '''For''' loop, many collections like arrays, vectors, and ranges, implement the '''Iterator'''  
 
* In a '''For''' loop, many collections like arrays, vectors, and ranges, implement the '''Iterator'''  
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| '''Slide 8'''
 
|| '''Slide 8'''
  
Line 239: Line 237:
 
A range with two dots like 0..11 is inclusive on the left.
 
A range with two dots like 0..11 is inclusive on the left.
  
i.e it starts at 0 and exclusive on the right. i.e ends at 10
+
That is it starts at 0 and exclusive on the right. That is ends at 10
  
 
This program will print numbers from 0 to 10.
 
This program will print numbers from 0 to 10.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
||  
 
||  
 
|| Next let us see an example of a for loop with an iterator method.
 
|| Next let us see an example of a for loop with an iterator method.
  
Clear the code window and copy and paste the code from the code file.
+
Clear the code window and copy and paste the code from the Code file.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| fn main() {
 
|| fn main() {
  
Line 261: Line 259:
 
|| In this code, we have an array '''‘data’''' containing five integers.  
 
|| In this code, we have an array '''‘data’''' containing five integers.  
  
The '''data.iter() '''method creates an iterator for the array '''data'''.
+
The '''data.iter() ''' method creates an iterator for the array '''data'''.
  
 
The '''for''' loop takes each element produced by the iterator and binds it to the variable '''data'''.  
 
The '''for''' loop takes each element produced by the iterator and binds it to the variable '''data'''.  
  
Inside the for loop, '''println''' is used to print the value of each element.
+
Inside the '''for''' loop, '''println''' is used to print the value of each element.
  
 
Save the file.
 
Save the file.
  
 
Let us execute the program.
 
Let us execute the program.
|- 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'''
+
|| In the terminal, type '''cargo run'''.
  
We can see the array elements are printed in sequence.
+
We can see the array elements are printed in the sequence.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
||  
 
||  
 
|| We will learn some more concepts that work with a '''for''' loop.
 
|| We will learn some more concepts that work with a '''for''' loop.
  
Let us see how to use a '''reverse''' method in the for loop.
+
Let us see how to use a '''reverse''' method in the '''for''' loop.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| '''fn''' main() {
 
|| '''fn''' main() {
  
Line 293: Line 291:
 
|| Copy and paste the code from the code file.
 
|| Copy and paste the code from the code file.
  
<span style="color:#000000;">If we want the range to include 11 as well, we can write it as </span><span style="color:#000000;">'''(1..</span><nowiki>=11).</nowiki>'''  
+
If we want the range to include 11 as well, we can write it as '''(1..<nowiki>=11).</nowiki>'''  
  
 
This is known as an inclusive range.
 
This is known as an inclusive range.
Line 299: Line 297:
 
The '''rev() '''method is applied to the range to produce a reverse iterator.  
 
The '''rev() '''method is applied to the range to produce a reverse iterator.  
  
This means the numbers will be fetched in reverse order.
+
This means the number will be fetched in reverse order.
  
 
Save the file.
 
Save the file.
  
 
Let us check the output.
 
Let us check 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 output displayed in the reverse order as expected.
 
We can see the output displayed in the reverse order as expected.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| '''Slide 11'''
 
|| '''Slide 11'''
  
Line 315: Line 313:
  
 
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 12'''
 
|| '''Slide 12'''
  
Line 322: Line 320:
  
 
Write a program to print odd numbers between 11 and 1 using a for loop
 
Write a program to print odd numbers between 11 and 1 using a for loop
*Hint: use Rev() method</div>
+
* Hint: use Rev() method
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| '''Slide 18'''
 
|| '''Slide 18'''
| style="color:#000000;" | Thank you for joining.
+
||Thank you for joining.
 
|-
 
|-
 
|}
 
|}
<div style="margin-left:-1.588cm;margin-right:-1.429cm;"></div>
 

Latest revision as of 15:07, 12 August 2025

Visual Cue Narration
Slide 1 Welcome to the Spoken Tutorial on Control Flow in Rust.
Slide 2

Learning Objectives

In this tutorial, we will learn about
  • Control Flow Statement
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
Slide 4

Prerequisites

To follow this tutorial,
  • You should be familiar with compiling and running a Rust program.
Slide 5

Code Files

  • The following code file is required to practise this tutorial
  • This file is provided in the Code Files link of this tutorial page
We will see about conditional statements in Rust.
Slide 6

Conditional Statements

Rust supports
  • if
  • else if
  • else

statements for making decisions based on conditions.

It always returns a boolean value.

Open Visual code editor Let us open the visual code editor and understand the control statements with examples.
In the menu bar, click on Terminal and select New Terminal

We can see a terminal window at the bottom.

> cd MyRustProject

> cargo new functions

Go to our working directory MyRustProject as explained earlier.

Type the command cargo new controlflow and press Enter

Open the created project as shown.

In the main.rs file, copy and paste the code from the code file.
fn main() {

let n = 100;

if n>100{

println!("Greater than 100");

}else if n==100{

println!("Equal to 100");

}else {

println!("Smaller than 100");

}

}

Let us look at this example for an if else statement.

If n is greater than 100, it prints a “greater than 100” statement.

If n equals 100, the second print statement will be executed.

If n is less than 100, it prints “smaller than 100”.

You can include multiple else if statements to check multiple conditions in sequence.

The condition must be true to run the code wrapped to it.

Press Ctrl and S to save the file.

Now let us run the program.

In the menu bar, click on Terminal and select New Terminal. In the menu bar, click on Terminal and select New Terminal.
> cargo run In the terminal, type cargo run to see the output.

The output shows “equal to 100” as we assigned n as 100 in the program.

Next let us see loop statements in Rust.
Slide

Loop

  • A loop is used to repeat a block of code until the specified condition is met
  • The loop keyword creates an infinite loop unless you explicitly break out of it
  • Rust provides several ways to loop, including loop, while, and for
Let us see an example for a loop program.

Clear the code window and replace the code from the Code file as shown.

fn main() {

let mut count = 0; loop { count += 1; println!("Count: {}", count); if count == 5 { break; } } }

This example has a mutable variable named count which is assigned to the value 0.

A loop is created in which it increases the count variable by 1.

It prints the value of count in the current iteration.

It will also check for the if statement condition.

When the variable count will be equal to 5, it will stop executing and come out of the loop.

If there is no if statement, it will execute infinitely.

Press Ctrl and S to save the file.

Let us run the program.

In the terminal, type cargo run

We can see the count variable is printed from 1 to 5.

It stops executing when the count reaches 5.

Next we will see how the while loop works.

Replace the code from the code file with the while loop program.

fn main() {

let mut counter = 1;

// usage of while loop

while counter < 6 {

println!("{}", counter);

counter += 1;

}

}

The while loop runs as long as a condition is true.

Here, the loop keeps running till the counter variable is less than 6.

Inside the loop, we are increasing the value of the counter by 1.

After the 5th iteration, the value of the counter will be 6.

So the condition, counter < 6 becomes false and the loop is terminated.

Save the program.

In the terminal, type cargo run

Check the output.

Next we will see about For loop.
Slide 7

For loop

  • The for loop iterates over a range or a collection
  • An iterator which provides a way to access elements one at a time.
  • In a For loop, many collections like arrays, vectors, and ranges, implement the Iterator
Slide 8

For loop -example fn main() { for n in 0..11 { println!("{}", n);

}

}

The For loop has a definite start and endpoint with increment for each iteration.

A range with two dots like 0..11 is inclusive on the left.

That is it starts at 0 and exclusive on the right. That is ends at 10

This program will print numbers from 0 to 10.

Next let us see an example of a for loop with an iterator method.

Clear the code window and copy and paste the code from the Code file.

fn main() {

let data = [2, 1, 17, 99, 34, 56];

for i in data.iter(){

println!("{}", i);

}

}

In this code, we have an array ‘data’ containing five integers.

The data.iter() method creates an iterator for the array data.

The for loop takes each element produced by the iterator and binds it to the variable data.

Inside the for loop, println is used to print the value of each element.

Save the file.

Let us execute the program.

In the terminal, type cargo run. In the terminal, type cargo run.

We can see the array elements are printed in the sequence.

We will learn some more concepts that work with a for loop.

Let us see how to use a reverse method in the for loop.

fn main() {

for i in (1..=11).rev() {

println!("{i}...");

} println!("Launch!");

}

Copy and paste the code from the code file.

If we want the range to include 11 as well, we can write it as (1..=11).

This is known as an inclusive range.

The rev() method is applied to the range to produce a reverse iterator.

This means the number will be fetched in reverse order.

Save the file.

Let us check the output.

In the terminal, type cargo run

We can see the output displayed in the reverse order as expected.

Slide 11 This brings us to the end of this tutorial.

Let us summarize.

Slide 12

Assignment

As an Assignment, do the following:

Write a program to print odd numbers between 11 and 1 using a for loop

  • Hint: use Rev() method
Slide 18 Thank you for joining.

Contributors and Content Editors

Madhurig, Nirmala Venkat