Difference between revisions of "Rust-Programming-Language/C2/Functions-and-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:#000000...")
 
Line 5: Line 5:
 
|-
 
|-
 
|| '''Slide 1'''
 
|| '''Slide 1'''
|| <span style="color:#000000;">Welcome to the Spoken Tutorial on </span><span style="color:#000000;">'''Functions and Control Flow '''</span><span style="color:#000000;">in </span><span style="color:#000000;">'''Rust.'''</span>
+
|| Welcome to the Spoken Tutorial on '''Functions and 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
* <div style="color:#000000;margin-left:1.27cm;margin-right:0cm;">Control Flow Statement and</div>
+
* Control Flow Statement and
* <div style="color:#000000;margin-left:1.27cm;margin-right:0cm;">Functions</div>
+
* Functions
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.153cm;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;margin-left:1.27cm;margin-right:0cm;">Ubuntu Linux OS version 22.04</div>
+
* Ubuntu Linux OS version 22.04
* <div style="margin-left:1.27cm;margin-right:0cm;"><span style="color:#000000;">Rust 1.</span><span style="color:#000000;">80</span><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.153cm;padding-right:0.191cm;"
+
|-  
|| <span style="color:#000000;">'''Slide </span>4'''
+
|| '''Slide 4'''
  
<div style="color:#000000;">'''Prerequisites'''</div>
+
'''Prerequisites'''
 
||
 
||
* <div style="color:#000000;margin-left:1.27cm;margin-right:0cm;">You should be familiar with compiling and running Rust files</div>
+
* You should be familiar with compiling and running Rust files
* <div style="margin-left:1.27cm;margin-right:0cm;"><span style="color:#000000;">If not go through the </span><span style="color:#000000;">prerequisite</span><span style="color:#000000;"> Rust tutorial on this website</span></div>
+
* If not go through the prerequisite Rust tutorial on this website
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.153cm;padding-right:0.191cm;"
+
|-  
|| <span style="color:#000000;">'''Slide'''</span> '''5'''
+
|| '''Slide''' '''5'''
  
<div style="color:#000000;">'''Code Files'''</div>
+
'''Code Files'''
 
||
 
||
* <div style="margin-left:1.27cm;margin-right:0cm;"><span style="color:#000000;">The files used in this tutorial are provided in the </span><span style="color:#000000;">'''Code files'''</span><span style="color:#000000;"> link of this tutorial page</span></div>
+
* The files used in this tutorial are provided in the '''Code files''' link of this tutorial page
* <div style="color:#000000;margin-left:1.27cm;margin-right:0cm;">Please download and extract the files.</div>
+
* Please download and extract the files.
* <div style="color:#000000;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.
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.153cm;padding-right:0.191cm;"
+
|-  
| style="color:#000000;" |  
+
||
| style="color:#000000;" | First we will see about conditional statements in Rust.
+
||First we will see about conditional statements in Rust.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.153cm;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;margin-left:1.27cm;margin-right:0cm;">if </div>
+
* if  
* <div style="color:#000000;margin-left:1.27cm;margin-right:0cm;">else if </div>
+
* else if  
* <div style="margin-left:1.27cm;margin-right:0cm;"><span style="color:#000000;">else</span><span style="color:#000000;">''' '''</span></div>
+
* 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 73: Line 73:
  
 
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() {
 +
let n = 100;
 +
if n>100{
 +
println!("Greater than 100");  
 +
}else if n==100{
 +
println!("Equal to 100");
 +
}else {
 +
println!("Smaller than 100");
 +
}
  
<div style="color:#000000;"></div>
+
}
|| <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>
+
|| Let us look at this example for an''' if else''' statement.
|- 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>
+
<div style="color:#000000;">if n>100{ </div>
+
<div style="color:#000000;">println!("Greater than 100"); </div>
+
<div style="color:#000000;">}else if n==100{ </div>
+
<div style="color:#000000;">println!("Equal to 100"); </div>
+
<div style="color:#000000;">}else { </div>
+
<div style="color:#000000;">println!("Smaller than 100"); </div>
+
<div style="color:#000000;">} </div>
+
  
<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.'''
  
|| <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>
+
|| 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;"
+
|-
 
|| > 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 have assigned '''n '''as 100 in the program.
 
The output shows “equal to 100” as we have 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;"
+
|-
|| <div style="color:#000000;">Slide</div>
+
|| Slide
  
<div style="color:#000000;">Loop</div>
+
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;"><span style="color:#000000;">Rust provides several ways to loop, including </span><span style="color:#000000;">'''loop'''</span><span style="color:#000000;">, </span><span style="color:#000000;">'''while'''</span><span style="color:#000000;">, and </span><span style="color:#000000;">'''for'''</span></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;"
+
|-
| style="color:#000000;" |  
+
||
 
|| Let us see an example for a '''loop''' program.
 
|| Let us see an example for a '''loop''' program.
  
Clear the code window and r<span style="color:#000000;">eplace the code from the code file as shown</span>.
+
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;"
+
|-
|| <div style="color:#000000;">fn main() {</div>
+
|| fn main() {
<div style="color:#000000;">let mut count = 0;</div>
+
let mut count = 0;
<div style="color:#000000;">loop {</div>
+
loop {
<div style="color:#000000;">count += 1;</div>
+
count += 1;
<div style="color:#000000;">println!("Count: {}", count);</div>
+
println!("Count: {}", count);
<div style="color:#000000;">if count == 5 {</div>
+
if count == 5 {
<div style="color:#000000;">break;</div>
+
break;
<div style="color:#000000;">}</div>
+
}
<div style="color:#000000;">}</div>
+
}
<div style="color:#000000;">}</div>
+
}
|| <span style="color:#000000;">This example has a mutable variable named </span><span style="color:#000000;">'''count'''</span><span style="color:#000000;"> which is assigned to the value 0.</span>
+
|| This example has a mutable variable named '''count''' which is assigned to the value 0.
<span style="color:#000000;">A loop is created in which it increases the </span><span style="color:#000000;">'''count'''</span><span style="color:#000000;"> variable by 1</span>.
+
A loop is created in which it increases the '''count''' variable by 1.
  
It<span style="color:#000000;"> prints the value of count in the current iteration. </span>
+
It prints the value of count in the current iteration.  
  
<span style="color:#000000;">It will also check for the </span><span style="color:#000000;">'''if '''</span><span style="color:#000000;">statement condition. </span>
+
It will also check for the '''if '''statement condition.  
  
<div style="color:#000000;">When the variable count will be equal to 5, it will stop executing and come out of the loop. </div>
+
When the variable count will be equal to 5, it will stop executing and come out of the loop.  
  
<span style="color:#000000;">If there is no if statement, it will execute infinitely.Press </span><span style="color:#000000;">'''ctrl and s '''</span><span style="color:#000000;">to save the file.</span>
+
If there is no if statement, it will execute infinitely.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;"
+
|-
| style="color:#000000;" |  
+
||
|| <span style="color:#000000;">In the terminal, type </span><span style="color:#000000;">'''cargo run'''</span>
+
|| In the terminal, type '''cargo run'''
  
 
We can see the count variable is printed from 1 to 5.  
 
We can see the count variable is printed from 1 to 5.  
  
 
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;"
+
|-
| style="color:#000000;" |  
+
||
|| <span style="color:#000000;">Next we will </span>see how<span style="color:#000000;"> the </span><span style="color:#000000;">'''while'''</span><span style="color:#000000;"> loop works.</span>
+
|| Next we will see how the '''while''' loop works.
  
Replace<span style="color:#000000;"> the code from the codefile with the </span><span style="color:#000000;">'''while'''</span><span style="color:#000000;"> loop program.</span>
+
Replace the code from the codefile with the '''while''' loop program.
|- 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>
+
|| fn main() {
  
<div style="color:#000000;">let mut counter = 1;</div>
+
let mut counter = 1;
<div style="color:#000000;">// usage of while loop</div>
+
// usage of while loop
<div style="color:#000000;">while counter < 6 {</div>
+
while counter < 6 {
<div style="color:#000000;">println!("{}", counter);</div>
+
println!("{}", counter);
<div style="color:#000000;">counter += 1;</div>
+
counter += 1;
<div style="color:#000000;">}</div>
+
}
<div style="color:#000000;">}</div>
+
}
|| <span style="color:#000000;">The </span><span style="color:#000000;">'''while'''</span><span style="color:#000000;"> loop runs as long as a condition is true.</span>
+
|| The '''while''' loop runs as long as a condition is true.
  
<span style="color:#000000;">Here, the loop keeps running till the </span><span style="color:#000000;">'''counter'''</span><span style="color:#000000;"> variable is less than 6. </span>
+
Here, the loop keeps running till the '''counter''' variable is less than 6.  
  
<div style="color:#000000;">Inside the loop, we are increasing the value of the counter by 1.</div>
+
Inside the loop, we are increasing the value of the counter by 1.
  
After the 5th<span style="color:#000000;"> iteration, the value </span>of the counter<span style="color:#000000;"> will be 6.</span>
+
After the 5th iteration, the value of the counter will be 6.
  
<div style="color:#000000;">So the condition, counter < 6 becomes false and the loop is terminated.</div>
+
So the condition, counter < 6 becomes false and the loop is terminated.
  
<div style="color:#000000;">Save the program.</div>
+
Save 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;">In the terminal, type </span><span style="color:#000000;">'''cargo run'''</span>
+
|| In the terminal, type '''cargo run'''
  
<div style="color:#000000;">Check the output.</div>
+
Check the output.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
| style="color:#000000;" |  
+
||
|| <span style="background-color:#ffffff;color:#000000;">Next we will see </span><span style="background-color:#ffffff;">about </span><span style="background-color:#ffffff;">'''For'''</span><span style="background-color:#ffffff;color:#000000;"> 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;"
+
|-
|| <span style="color:#000000;">'''Slide</span> 7'''
+
|| '''Slide 7'''
  
<div style="color:#000000;">'''For loop'''</div>
+
'''For loop'''
 
||
 
||
* <div style="margin-left:1.27cm;margin-right:0cm;"><span style="color:#000000;">The </span><span style="color:#000000;">'''for'''</span><span style="color:#000000;"> </span><span style="color:#000000;">loop iterates over a range or a collection</span></div>
+
* The '''for''' loop iterates over a range or a collection
* <div style="margin-left:1.27cm;margin-right:0cm;"><span style="color:#000000;">An </span><span style="color:#000000;">'''iterator '''</span><span style="color:#000000;">which provides a way to access elements one at a time.</span></div>
+
* An '''iterator '''which provides a way to access elements one at a time.
* <div style="margin-left:1.27cm;margin-right:0cm;"><span style="color:#000000;">In a </span><span style="color:#000000;">'''For'''</span><span style="color:#000000;"> loop, many collections like arrays, vectors, and ranges, implement the </span><span style="color:#000000;">'''Iterator'''</span><span style="color:#000000;"> </span></div>
+
* 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;"
+
|-
|| <span style="color:#000000;">'''Slide</span> 8'''
+
|| '''Slide 8'''
  
<div style="color:#000000;">'''For loop -example'''</div>
+
'''For loop -example'''
  
<span style="color:#000000;">'''fn'''</span><span style="color:#000000;"> main() {</span>
+
'''fn''' main() {
<span style="color:#000000;">'''for'''</span><span style="color:#000000;"> n </span><span style="color:#000000;">'''in'''</span><span style="color:#000000;"> 0..11 {</span>
+
'''for''' n '''in''' 0..11 {
<div style="color:#000000;">println!("{}", n);</div>
+
println!("{}", n);
<div style="color:#000000;">}</div>
+
}
<div style="color:#000000;">}</div>
+
}
  
|| The''' For'''<span style="color:#000000;"> loop has a definite </span>start<span style="color:#000000;"> and endpoint with increment for each iteration.</span>
+
|| The''' For''' loop has a definite start and endpoint with increment for each iteration.
  
<div style="color:#000000;">A range with two dots like 0..11 is inclusive on the left.</div>
+
A range with two dots like 0..11 is inclusive on the left.
  
<div style="color:#000000;">i.e it starts at 0 and exclusive on the right. i.e ends at 10</div>
+
i.e it starts at 0 and exclusive on the right. i.e ends at 10
  
<span style="color:#000000;">This program will</span><span style="color:#000000;"> </span><span style="color:#000000;">print numbers from 0 to 10</span>
+
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;"
+
|-
|| <div style="color:#000000;">fn main() {</div>
+
|| fn main() {
  
<div style="color:#000000;">let data = [2, 1, 17, 99, 34, 56];</div>
+
let data = [2, 1, 17, 99, 34, 56];
<div style="color:#000000;">// iterator</div>
+
// iterator
<div style="color:#000000;">//let numbers_iterator = data.iter();</div>
+
//let numbers_iterator = data.iter();
<div style="color:#000000;">for i in data.iter(){</div>
+
for i in data.iter(){
<div style="color:#000000;">println!("{}", i);</div>
+
println!("{}", i);
<div style="color:#000000;">}</div>
+
}
<div style="color:#000000;">}</div>
+
}
|| <div style="color:#000000;"></div>
+
  
<span style="color:#000000;">In this code, we have an array </span><span style="color:#000000;">'''‘data’'''</span><span style="color:#000000;"> containing five integers. </span>
+
||  In this code, we have an array '''‘data’''' containing five integers.  
  
<span style="color:#000000;">The </span><span style="color:#000000;">'''data.iter() '''</span><span style="color:#000000;">method creates an iterator for the array </span><span style="color:#000000;">'''data'''</span><span style="color:#000000;">.</span>
+
The '''data.iter() '''method creates an iterator for the array '''data'''.
  
<span style="color:#000000;">The </span><span style="color:#000000;">'''for'''</span><span style="color:#000000;"> loop takes each element produced by the iterator and binds it to the variable </span><span style="color:#000000;">'''data'''</span><span style="color:#000000;">. </span>
+
The '''for''' loop takes each element produced by the iterator and binds it to the variable '''data'''.  
  
<span style="color:#000000;">Inside the for loop, </span><span style="color:#000000;">'''println'''</span><span style="color:#000000;"> is used to print the value of each element.</span>
+
Inside the for loop, '''println''' is used to print the value of each element.
  
<div style="color:#000000;">Save the file.</div>
+
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;"
+
|-
| style="color:#000000;" |  
+
||
|| <span style="color:#000000;">In the terminal, type </span><span style="color:#000000;">'''cargo run'''</span>
+
|| 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 sequence.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
| style="color:#000000;" |  
+
||
 
|| 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;"
+
|-
|| <span style="color:#000000;">'''fn'''</span><span style="color:#000000;"> main() {</span>
+
|| '''fn''' main() {
  
<div style="color:#252525;">for i in (1..=11).rev() {</div>
+
<div style="color:#252525;">for i in (1..=11).rev() {
<div style="color:#252525;">println!("{i}...");</div>
+
<div style="color:#252525;">println!("{i}...");
<div style="color:#252525;">}</div>
+
<div style="color:#252525;">}
<div style="color:#252525;">println!("Launch!");</div>
+
<div style="color:#252525;">println!("Launch!");
<div style="color:#000000;">}</div>
+
}
 
|| Copy and paste the code from the code file.
 
|| Copy and paste the code from the code file.
  
Line 281: Line 278:
 
This means the numbers will be fetched in reverse order.
 
This means the numbers will be fetched in reverse order.
  
<div style="color:#252525;">save the file.</div>
+
<div style="color:#252525;">save the file.
  
<div style="color:#252525;">Let us check the output</div>
+
<div style="color:#252525;">Let us check the output
|- 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 terminal, type </span><span style="color:#000000;">'''cargo run'''</span>
+
|| 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;"
+
|-
| style="color:#000000;" |  
+
||
 
|| Next we will see how to print odd numbers using a for loop.
 
|| Next we will see how to print odd numbers using a for loop.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
|| <span style="color:#000000;">'''fn'''</span><span style="color:#000000;"> main() {</span>
+
|| '''fn''' main() {
<div style="color:#000000;">for i in (1..11).rev() {</div>
+
for i in (1..11).rev() {
<div style="color:#000000;">if i % 2 == 0 {</div>
+
if i % 2 == 0 {
<div style="color:#000000;">continue;</div>
+
continue;
<div style="color:#000000;">}</div>
+
}
<div style="color:#000000;">println!("{i}...");</div>
+
println!("{i}...");
<div style="color:#000000;">}</div>
+
}
<div style="color:#000000;">println!("Launch!");</div>
+
println!("Launch!");
<div style="color:#000000;">}</div>
+
}
|| <div style="color:#252525;">Copy and paste the code from the code file</div>
+
|| <div style="color:#252525;">Copy and paste the code from the code file
  
 
The''' if '''statement checks if''' i '''is divisible by 2.
 
The''' if '''statement checks if''' i '''is divisible by 2.
Line 311: Line 308:
  
 
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;"
+
|-
| style="color:#000000;" |  
+
||
 
|| In the terminal, check the output.
 
|| In the terminal, check the output.
  
 
We can see the odd numbers are printed between 11 and 1.
 
We can see the odd numbers are printed between 11 and 1.
|- 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 we will see about </span><span style="color:#000000;">'''Functions'''</span><span style="color:#000000;"> in Rust.</span>
+
|| Next we will see about '''Functions''' in Rust.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
|| <span style="color:#000000;">'''Slide</span> 9'''
+
|| '''Slide 9'''
  
<div style="color:#000000;">'''Functions'''</div>
+
'''Functions'''
 
||
 
||
* <div style="margin-left:1.27cm;margin-right:0cm;">It <span style="color:#000000;">is a set of statements to perform a specific task</span></div>
+
* It is a set of statements to perform a specific task
* <div style="color:#000000;margin-left:1.27cm;margin-right:0cm;">Functions organize the program into logical blocks of code.</div>
+
* Functions organize the program into logical blocks of code.
* <div style="margin-left:1.27cm;margin-right:0cm;"><span style="color:#000000;">Functions </span>make<span style="color:#000000;"> the code reusable.</span></div>
+
* Functions make the code reusable.
* <div style="margin-left:1.27cm;margin-right:0cm;"><span style="color:#000000;">It </span>structures<span style="color:#000000;"> your programs and </span>makes<span style="color:#000000;"> them more readable and maintainable.</span></div>
+
* It structures your programs and makes them more readable and maintainable.
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
|| <div style="color:#000000;">'''Slide 10'''</div>
+
|| '''Slide 10'''
<div style="color:#000000;">'''Function - Example'''</div>
+
'''Function - Example'''
<div style="color:#000000;">// define a function</div>
+
// define a function
<div style="color:#000000;">fn greet() {</div>
+
fn greet() {
<div style="color:#000000;">println!("Hello, World!");</div>
+
println!("Hello, World!");
<div style="color:#000000;">}</div>
+
}
<div style="color:#000000;">fn main() {</div>
+
fn main() {
<div style="color:#000000;">//function call</div>
+
//function call
<div style="color:#000000;">greet();</div>
+
greet();
<div style="color:#000000;">}</div>
+
}
|| <div style="color:#000000;">Here, we have created a''' greet()''' function that prints "Hello, World!" .</div>
+
|| Here, we have created a''' greet()''' function that prints "Hello, World!" .
  
<div style="color:#000000;">To invoke the created function, we need to make a function call.</div>
+
To invoke the created function, we need to make a function call.
  
<div style="color:#000000;">Notice that we are calling the function from inside '''main()'''.</div>
+
Notice that we are calling the function from inside '''main()'''.
  
<div style="color:#000000;">In Rust, '''main() '''is also a function known as a built-in function that has a special meaning. </div>
+
In Rust, '''main() '''is also a function known as a built-in function that has a special meaning.  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
| style="color:#000000;" |  
+
||
| style="color:#000000;" | Next we will see how we can create a function with parameters.
+
||Next we will see how we can create a function with parameters.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
| style="color:#000000;" |  
+
||
|| <div style="color:#000000;">Let us understand the working of functions with examples.</div>
+
|| Let us understand the working of functions with examples.
  
<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, replace the </span><span style="background-color:#ffffff;color:#000000;">'''code'''</span><span style="background-color:#ffffff;color:#000000;"> </span><span style="background-color:#ffffff;">from the code file.</span>
+
In the '''main.rs '''file, replace 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;"
+
|-
|| <div style="color:#000000;">// define an add function that takes in two parameters</div>
+
|| // define an add function that takes in two parameters
<div style="color:#000000;">fn add(a: i32, b: i32) {</div>
+
fn add(a: i32, b: i32) {
<div style="color:#000000;">let sum = a + b;</div>
+
let sum = a + b;
<div style="color:#000000;">println!("Sum of a and b = {}", sum);</div>
+
println!("Sum of a and b = {}", sum);
<div style="color:#000000;">}</div>
+
}
<div style="color:#000000;">fn main() {</div>
+
fn main() {
<div style="color:#000000;">// call add function with arguments</div>
+
// call add function with arguments
<div style="color:#000000;">add(12, 10);</div>
+
add(12, 10);
<div style="color:#000000;">}</div>
+
}
 
|| In this example, the add function takes 2 parameters and gives the sum as output.
 
|| In this example, the add function takes 2 parameters and gives the sum as output.
  
<div style="color:#000000;">Here, a and b are function parameters.</div>
+
Here, a and b are function parameters.
<div style="color:#000000;">i32 is the data type of parameters.</div>
+
i32 is the data type of parameters.
<span style="color:#000000;">12 and 10 are known as function arguments that are passed to the </span><span style="color:#000000;">'''add'''</span><span style="color:#000000;"> function.</span>
+
12 and 10 are known as function arguments that are passed to the '''add''' function.
  
<div style="color:#000000;">That means 12 is assigned to a and 10 is assigned to b.</div>
+
That means 12 is assigned to a and 10 is assigned to b.
  
<div style="color:#000000;">Save the file.</div>
+
Save the file.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
| style="color:#000000;" |  
+
|| In the terminal, type''' cargo run '''.
|| <span style="color:#000000;">In the terminal, type</span><span style="color:#000000;">''' cargo run '''</span><span style="color:#000000;">and see the output.</span>
+
|| In the terminal, type''' cargo run '''and see the output.
  
 
It prints the sum of a and b as 22.
 
It prints the sum of a and b as 22.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
 
||  
 
||  
 
|| Next we will see an example for a function with return value.
 
|| Next we will see an example for a function with return value.
  
 
Replace the code as shown.
 
Replace the code as shown.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
||  
+
|| fn area(length: i32, breadth: i32) -> i32 {
 
+
fn area(length: i32, breadth: i32) -> i32 {
+
  
 
&nbsp;&nbsp;&nbsp;length * breadth
 
&nbsp;&nbsp;&nbsp;length * breadth
Line 403: Line 398:
  
 
Change the program to return length*breadth
 
Change the program to return length*breadth
||
 
  
In this example, we define a function to calculate the area of a rectangle.  
+
||  In this example, we define a function to calculate the area of a rectangle.  
  
 
The area function takes two parameters, length and breadth, both of type i32.  
 
The area function takes two parameters, length and breadth, both of type i32.  
Line 429: Line 423:
  
  
<span style="background-color:#ffffff;">This time we get an error, </span><span style="background-color:#ffffff;">'''remove this semicolon to return this value.'''</span>
+
This time we get an error, '''remove this semicolon to return this value.'''
  
 
This is because it is being considered as a '''statement''' and not as a '''return''' value.
 
This is because it is being considered as a '''statement''' and not as a '''return''' value.
  
 
You can use either conventions&nbsp;
 
You can use either conventions&nbsp;
* <div style="margin-left:1.247cm;margin-right:0cm;">length*breadth without semicolon or</div>
+
* length*breadth without semicolon or
* <div style="margin-left:1.247cm;margin-right:0cm;">return length*breadth with semicolon</div>
+
* return length*breadth with semicolon
 
for returning the value.
 
for returning the value.
 
Save the file and run the program
 
Save the file and run the program
  
 
We can observe that the area of the rectangle is 30.
 
We can observe that the area of the rectangle is 30.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
| style="color:#000000;" |  
+
||
|| <div style="color:#000000;">This brings us to the end of this tutorial.</div>
+
|| This brings us to the end of this tutorial.
  
<div style="color:#000000;">Let us summarize. </div>
+
Let us summarize.  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
 
|| '''Slide 11'''
 
|| '''Slide 11'''
  
Line 451: Line 445:
 
|| In this tutorial, we learn about
 
|| In this tutorial, we learn about
  
* <div style="margin-left:1.27cm;margin-right:0cm;">Control Flow Statement and</div>
+
* Control Flow Statement and
* <div style="margin-left:1.27cm;margin-right:0cm;">Functions</div>
+
* Functions
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
 
|| '''Slide 12'''
 
|| '''Slide 12'''
  
Line 460: Line 454:
 
'''Assignment'''
 
'''Assignment'''
 
|| As an Assignment, do the following:
 
|| As an Assignment, do the following:
* <div style="margin-left:1.27cm;margin-right:0cm;"><span style="background-color:#ffffff;">In the </span><span style="background-color:#ffffff;">'''main.rs'''</span><span style="background-color:#ffffff;"> file</span></div>
+
* In the '''main.rs''' file
** <div style="margin-left:2.54cm;margin-right:0cm;">Create a '''function''' that accepts a '''number '''as a''' parameter'''</div>
+
** Create a '''function''' that accepts a '''number '''as a''' parameter'''
** <div style="margin-left:2.54cm;margin-right:0cm;">Print '''EVEN '''if the number is even</div>
+
** Print '''EVEN '''if the number is even
** <div style="margin-left:2.54cm;margin-right:0cm;">Else print '''ODD'''</div>
+
** Else print '''ODD'''
* <div style="margin-left:1.27cm;margin-right:0cm;">'''Compile '''and '''execute '''the project.</div>
+
* '''Compile '''and '''execute '''the project.
* <div style="margin-left:1.27cm;margin-right:0cm;">Observe the output in the '''Terminal'''</div>
+
* Observe the output in the '''Terminal'''
  
 
|-
 
|-
| style="border:1pt solid #252525;padding-top:0.176cm;padding-bottom:0.176cm;padding-left:0.123cm;padding-right:0.176cm;" | '''Slide 13'''
+
|| '''Slide 13'''
  
<div style="color:#000000;">'''About Spoken Tutorial Project'''</div>
+
'''About Spoken Tutorial Project'''
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;" | <span style="color:#000000;">This video </span>s<span style="color:#000000;">ummarizes the Spoken Tutorial project.</span>
+
||This video summarizes the Spoken Tutorial project.
  
<div style="color:#000000;">Please download and watch it.</div>
+
Please download and watch it.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
 
|| '''Slide 14'''
 
|| '''Slide 14'''
  
<div style="color:#000000;">'''Spoken Tutorial Workshops'''</div>
+
'''Spoken Tutorial Workshops'''
|| <span style="color:#000000;">The </span><span style="color:#000000;">'''Spoken Tutorial Project '''</span><span style="color:#000000;">Team conducts workshops and gives certificates.</span>
+
|| The '''Spoken Tutorial Project '''Team conducts workshops and gives certificates.
  
<div style="color:#000000;">For more details, please write to us.</div>
+
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;"
+
|-
|| <div style="color:#000000;">'''Slide 15'''</div>
+
|| '''Slide 15'''
  
<div style="color:#000000;">'''Forum for specific questions'''</div>
+
'''Forum for specific questions'''
| style="color:#000000;" | 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;"
+
|-
 
|| '''Slide 16'''
 
|| '''Slide 16'''
  
<div style="color:#000000;">'''Acknowledgement'''</div>
+
'''Acknowledgement'''
| style="color:#000000;" | Spoken Tutorial project was established at IIT Bombay by the Ministry of Education(MoE), Govt of India
+
||Spoken Tutorial project was established at IIT Bombay 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;"
+
|-
 
|| '''Slide 17'''
 
|| '''Slide 17'''
  
<div style="color:#000000;">'''Acknowledgement'''</div>
+
'''Acknowledgement'''
|| <span style="color:#000000;">We would like to thank </span><span style="color:#000000;">'''Vishal Pokuri from VIT Vellore '''</span><span style="color:#000000;">for content contribution.</span>
+
|| 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;"
+
|-
 
|| '''Slide 18'''
 
|| '''Slide 18'''
  
<div style="color:#000000;">'''Thank You'''</div>
+
'''Thank You'''
|| <span style="color:#000000;">This tutorial is contributed by </span><span style="color:#000000;">'''Nirmala Venkat'''</span><span style="color:#000000;"> and </span><span style="color:#000000;">'''Ketki Bhamble'''</span><span style="color:#000000;"> from the spoken tutorial team.</span>
+
|| This tutorial is contributed by '''Nirmala Venkat''' and '''Ketki Bhamble''' from the spoken tutorial team.
  
<div style="color:#000000;">Thank you for joining.</div>
+
Thank you for joining.
  
 
|-
 
|-
 
|}
 
|}
<div style="margin-left:-1.588cm;margin-right:-1.429cm;"></div>
 

Revision as of 11:29, 27 May 2025

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

Learning Objectives

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

  • You should be familiar with compiling and running Rust files
  • If not go through the prerequisite Rust tutorial on this website
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.
First 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 functions 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 have 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 codefile 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.

i.e it starts at 0 and exclusive on the right. i.e 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]; // iterator //let numbers_iterator = data.iter(); 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

We can see the array elements are printed in 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 numbers 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.

Next we will see how to print odd numbers using a for loop.
fn main() {

for i in (1..11).rev() { if i % 2 == 0 { continue; } println!("{i}..."); } println!("Launch!"); }

Copy and paste the code from the code file

The if statement checks if i is divisible by 2.

If this condition is true, the continue statement is executed.

The continue statement skips the rest of the loop body for the current iteration and moves to the next one.

Save the program

In the terminal, check the output.

We can see the odd numbers are printed between 11 and 1.

Next we will see about Functions in Rust.
Slide 9

Functions

  • It is a set of statements to perform a specific task
  • Functions organize the program into logical blocks of code.
  • Functions make the code reusable.
  • It structures your programs and makes them more readable and maintainable.
Slide 10

Function - Example // define a function fn greet() { println!("Hello, World!"); } fn main() { //function call greet(); }

Here, we have created a greet() function that prints "Hello, World!" .

To invoke the created function, we need to make a function call.

Notice that we are calling the function from inside main().

In Rust, main() is also a function known as a built-in function that has a special meaning.

Next we will see how we can create a function with parameters.
Let us understand the working of functions with examples.

In the main.rs file, replace the code from the code file.

// define an add function that takes in two parameters

fn add(a: i32, b: i32) { let sum = a + b; println!("Sum of a and b = {}", sum); } fn main() { // call add function with arguments add(12, 10); }

In this example, the add function takes 2 parameters and gives the sum as output.

Here, a and b are function parameters. i32 is the data type of parameters. 12 and 10 are known as function arguments that are passed to the add function.

That means 12 is assigned to a and 10 is assigned to b.

Save the file.

In the terminal, type cargo run . In the terminal, type cargo run and see the output.

It prints the sum of a and b as 22.

Next we will see an example for a function with return value.

Replace the code as shown.

fn area(length: i32, breadth: i32) -> i32 {

   length * breadth

}

fn main() {

    let k = area(5, 6);

    println!("Area of the rectangle = {k}");

}


Change the program to return length*breadth

In this example, we define a function to calculate the area of a rectangle.

The area function takes two parameters, length and breadth, both of type i32.

-> i32 - right arrow i32 before the opening curly bracket indicates the function's return type.

It returns an i32 value representing the area of the rectangle.

In the next line, notice the lack of a semicolon after length * breadth.

In Rust, the absence of a semicolon signifies that this line is an expression.

The result value is returned by the function. Save the file and run the program.

We can see the output as the area of the rectangle is 30.

Now let us add a semicolon at the end of length into Breadth statement .

Let us run the program and see how the program works.

It would not return a value and cause a compilation error.


This time we get an error, remove this semicolon to return this value.

This is because it is being considered as a statement and not as a return value.

You can use either conventions 

  • length*breadth without semicolon or
  • return length*breadth with semicolon

for returning the value. Save the file and run the program

We can observe that the area of the rectangle is 30.

This brings us to the end of this tutorial.

Let us summarize.

Slide 11

Summary

In this tutorial, we learn about
  • Control Flow Statement and
  • Functions
Slide 12


Assignment

As an Assignment, do the following:
  • In the main.rs file
    • Create a function that accepts a number as a parameter
    • Print EVEN if the number is even
    • Else print ODD
  • Compile and execute the project.
  • Observe the output in the Terminal
Slide 13

About Spoken Tutorial Project

This video summarizes the Spoken Tutorial project.

Please download and watch it.

Slide 14

Spoken Tutorial Workshops

The Spoken Tutorial Project Team conducts workshops and gives certificates.

For more details, please write to us.

Slide 15

Forum for specific questions

Please post your timed queries in this forum
Slide 16

Acknowledgement

Spoken Tutorial project was established at IIT Bombay by the Ministry of Education(MoE), Govt of India
Slide 17

Acknowledgement

We would like to thank Vishal Pokuri from VIT Vellore for content contribution.
Slide 18

Thank You

This tutorial is contributed by Nirmala Venkat and Ketki Bhamble from the spoken tutorial team.

Thank you for joining.