Difference between revisions of "Rust-Programming-Language/C2/Iterators-and-Closures/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
 
Line 1: Line 1:
 +
 
{| border="1"
 
{| border="1"
 
|-
 
|-
Line 5: Line 6:
 
|-
 
|-
 
|| '''Slide 1'''
 
|| '''Slide 1'''
|| Welcome to the Spoken Tutorial on '''Iterators and Closures''' in''' Rust.'''
+
 
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
'''Title Slide'''
 +
|| Welcome to the Spoken Tutorial on '''Iterators and Closures''' in''' Rust'''.
 +
|-  
 
|| '''Slide 2'''
 
|| '''Slide 2'''
  
 
'''Learning Objectives'''
 
'''Learning Objectives'''
 
|| In this tutorial, we will learn about:
 
|| In this tutorial, we will learn about:
* <div style="margin-left:1.27cm;margin-right:0cm;">Iterators </div>
+
* Iterators  
* <div style="margin-left:1.27cm;margin-right:0cm;">Types of Iterators</div>
+
* Types of Iterators and
* <div style="margin-left:1.27cm;margin-right:0cm;">Closures</div>
+
* Closures
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.101cm;padding-right:0.191cm;"
+
|-  
 
|| '''Slide 3'''
 
|| '''Slide 3'''
  
Line 23: Line 26:
 
|| To record this tutorial I’m using the following setup.
 
|| To record this tutorial I’m using the following setup.
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.101cm;padding-right:0.191cm;"
+
|-  
|| '''Slide''' '''5'''
+
|| '''Slide 4'''
  
 
'''Code Files'''
 
'''Code Files'''
 +
 +
* iterators.rs
 
||  
 
||  
* <div style="margin-left:1.27cm;margin-right:0cm;">The following code file is required to practise this tutorial.</div>
+
* The following code file is required to practise this tutorial.
* <div style="margin-left:1.27cm;margin-right:0cm;">iterators.rs</div>
+
* <div style="margin-left:1.27cm;margin-right:0cm;">This file is provided in the Code Files link of this tutorial page.</div>
+
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.101cm;padding-right:0.191cm;"
+
* This file is provided in the Code Files link of this tutorial page.
|| '''Slide 6:'''
+
 
 +
|-  
 +
|| '''Slide 5'''
  
 
'''Iterators'''
 
'''Iterators'''
  
 
|| Let us get started.
 
|| Let us get started.
* <div style="margin-left:1.27cm;margin-right:0cm;">Iterators are powerful and flexible tools.</div>
 
* <div style="margin-left:1.27cm;margin-right:0cm;">They process sequences of data one at a time.</div>
 
* <div style="margin-left:1.27cm;margin-right:0cm;">They help to iterate over a collection of values such as arrays, vectors, maps, etc.</div>
 
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.101cm;padding-right:0.191cm;"
+
* Iterators are powerful and flexible tools.
|| '''Slide 7:'''
+
* They process sequences of data one at a time.
 +
* They help to iterate over a collection of values such as arrays, vectors, maps, etc.
 +
 
 +
|-
 +
|| '''Slide 6 + 7'''
  
 
'''Type of Iterators'''
 
'''Type of Iterators'''
  
 
|| In Rust we can categorize iterators into 3 main types.
 
|| In Rust we can categorize iterators into 3 main types.
* <div style="margin-left:1.27cm;margin-right:0cm;">'''Basic & Range Iterators '''</div>
+
* '''Basic & Range Iterators '''
* <div style="margin-left:1.27cm;margin-right:0cm;">'''Iterator Adapters '''and</div>
+
* '''Iterator Adapters ''' and
* <div style="margin-left:1.27cm;margin-right:0cm;">'''Iterator Consumers/Collectors'''</div>
+
* '''Iterator Consumers/Collectors'''
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.101cm;padding-right:0.191cm;"
+
|-  
 
||  
 
||  
||  
+
|| Let’s learn about '''Basic and Range Iterators''' with an example.
 
+
|-  
Let’s learn about '''Basic and Range Iterators''' with an example.
+
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
 
|| Open '''Visual''' '''code editor'''
 
|| Open '''Visual''' '''code editor'''
 
|| Open the '''Visual Studio code editor.'''
 
|| 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;"
+
|-  
 
|| 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'''.
 
|| 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;"
+
|-  
 
|| I have created the '''filehandling''' project as explained earlier.
 
|| I have created the '''filehandling''' project as explained earlier.
 
|| Let us go to our working directory '''MyRustProject''' as explained earlier.
 
|| Let us go to our working directory '''MyRustProject''' as explained earlier.
Line 73: Line 76:
 
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;"
+
|-  
 
|| Point to the '''main.rs''' file.
 
|| Point to the '''main.rs''' file.
 
+
|| In the '''main.rs '''file, copy and paste the code from the Code file.
|| <span style="background-color:#ffffff;">In the </span><span style="background-color:#ffffff;">'''main.rs '''</span><span style="background-color:#ffffff;">file, 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;"
+
 
|| fn main() {
 
|| fn main() {
  
Line 104: Line 106:
  
 
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;"
+
|-  
 
||  
 
||  
 
|| Run the code to see the output.
 
|| Run the code to see the output.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
||  
 
||  
 
|| Let’s see an example of commonly used '''iterator adapters'''.
 
|| Let’s see an example of commonly used '''iterator adapters'''.
Line 114: Line 116:
  
 
Copy paste the code from the code file.
 
Copy 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 136: Line 138:
 
It gives us '''immutable references''' to each element in the vector.
 
It gives us '''immutable references''' to each element in the vector.
  
'''.map() '''method uses a '''closure''' that takes each element x and returns <span style="color:#000000;">its </span><span style="color:#000000;">square</span>
+
'''.map() '''method uses a '''closure''' that takes each element x and returns its square.
  
This doesn't immediately compute the <span style="color:#000000;">square</span>.
+
This doesn't immediately compute the square.
  
 
It builds a new iterator pipeline that knows how to compute them when needed.  
 
It builds a new iterator pipeline that knows how to compute them when needed.  
  
Finally, '''.collect() '''consumes the iterator and gathers the squares into a<span style="color:#ff0000;"> </span><span style="color:#000000;">new </span><span style="color:#000000;">vector</span>
+
Finally, '''.collect() ''' method consumes the iterator and gathers the squares into a new vector
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
||  
 
||  
 
|| Run the code to see the output.
 
|| Run the code to see the output.
  
The result is printed as a <span style="color:#000000;">new vector [1</span>, 4, 9, 16, 25]
+
The result is printed as a new vector [1, 4, 9, 16, 25]
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
||  
 
||  
 
|| Let’s see an example of '''Iterator Consumers '''or''' Collectors.'''  
 
|| Let’s see an example of '''Iterator Consumers '''or''' Collectors.'''  
  
These methods consume an iterator and return results like '''Vec, bool''', or a single value.
+
These methods consume an iterator and return results like '''Vec''', '''bool''', or a single value.
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
||  
 
||  
 
|| Copy and paste the code from the code file.
 
|| 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 175: Line 177:
 
}
 
}
  
|| This program shows how to use '''iterators '''with filters to show only even numbers from a vector.
+
|| This program shows how to use '''iterators ''' with filters to show only even numbers from a vector.
  
 
Filter() method which takes a closure that returns true or false for each element.
 
Filter() method which takes a closure that returns true or false for each element.
  
The condition that we’ve used here will check if the number is even or not.
+
The condition that we have used here will check if the number is even or not.
  
 
Only the elements that satisfy this condition are passed forward in the iterator pipeline.
 
Only the elements that satisfy this condition are passed forward in the iterator pipeline.
  
Lastly, .collect() method gathers the remaining elements into new '''vector evens'''.
+
Lastly, '''.collect()''' method gathers the remaining elements into new '''vector evens'''.
  
 
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 '''and see the output.
 
|| In the terminal, type '''cargo run '''and see the output.
  
 
The result is [2, 4, 6], it contains only even numbers from the original list.
 
The result is [2, 4, 6], it contains only even numbers from the original list.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
||
+
||
+
|| '''Slide 8'''
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|| '''Slide 6:'''
+
  
 
'''Closures'''
 
'''Closures'''
* <div style="margin-left:1.27cm;margin-right:0cm;">In Rust, closure is a function without names</div>
+
* In Rust, closure is a function without names
* <div style="margin-left:1.27cm;margin-right:0cm;">It can capture variables from its environment, which is a key difference from regular functions.</div>
+
* It can capture variables from its environment, which is a key difference from regular functions.
  
 
|| Next we will see about '''Closures'''.
 
|| Next we will see about '''Closures'''.
* <div style="margin-left:1.27cm;margin-right:0cm;">Closure is a function without names</div>
+
* Closure is a function without names
* <div style="margin-left:1.27cm;margin-right:0cm;">It can capture variables from its environment, which is a key difference from regular functions.</div>
+
* It can capture variables from its environment, which is a key difference from regular functions.
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
||  
 
||  
 
|| Let us understand '''closures''' with an example.
 
|| Let us understand '''closures''' with an example.
Line 212: Line 212:
  
 
Clear the window and copy and paste the code from the codefile.
 
Clear the window and 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;"
+
|-  
 
|| fn main() {
 
|| fn main() {
  
Line 226: Line 226:
  
 
'''}'''
 
'''}'''
|| Here, parameters are placed between '''vertical <span style="color:#000000;">pipelines</span><span style="color:#ff0000;">.'''</span>
+
|| Here, parameters are placed between '''vertical pipelines'''.
  
<span style="background-color:#ffffff;">We have defined a closure and binded it to the </span><span style="background-color:#ffffff;">'''add_one'''</span><span style="background-color:#ffffff;"> variable. </span>
+
We have defined a closure and binded it to the '''add_one''' variable.  
  
<span style="background-color:#ffffff;">We then call the closure with </span><span style="background-color:#ffffff;">'''add_one(2)'''</span><span style="background-color:#ffffff;"> and bind the return value to the </span><span style="background-color:#ffffff;">'''result'''</span><span style="background-color:#ffffff;"> variable.</span>
+
We then call the closure with '''add_one(2)''' and bind the return value to the '''result''' variable.
  
'''Closures '''can omit''' type annotations '''and '''return types''', as Rust is capable of '''type inference'''
+
'''Closures ''' can omit''' type annotations '''and '''return types''', as Rust is capable of '''type inference'''
  
 
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;"
+
|-  
 
||  
 
||  
 
|| Run the code to see the output.
 
|| Run the code to see the output.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
||  
 
||  
|| <span style="color:#000000;">Let us see another example for multiple statements inside a closure.</span><span style="color:#ff0000;"> </span>
+
|| Let us see another example for multiple statements inside a closure.  
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
||  
 
||  
 
|| Clear the previous code. Copy paste the code from the code file.
 
|| Clear the previous code. Copy 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 278: Line 278:
  
 
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.'''
Line 285: Line 285:
  
 
That is the sum of 5 and 3 is 8. The squared value is 64.
 
That is the sum of 5 and 3 is 8. The squared value is 64.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
||  
+
|| '''Slide 9'''
 +
 
 +
'''Summary'''
 
|| 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 8
+
||'''Slide 10 + 11'''
* <div style="margin-left:1.27cm;margin-right:0cm;">Write a Rust program that takes a vector of integers:</div>
+
 
 +
'''Assignment'''
 +
 
 +
* Write a Rust program that takes a vector of integers:
  
 
let numbers = vec![10, 15, 20, 25, 30, 35, 40];
 
let numbers = vec![10, 15, 20, 25, 30, 35, 40];
  
* <div style="margin-left:1.27cm;margin-right:0cm;">Using iterators, perform the following steps:</div>
+
* Using iterators, perform the following steps:
 
   
 
   
** <div style="margin-left:1.905cm;margin-right:0cm;">Filter out the numbers that are divisible by 10</div>
+
** Filter out the numbers that are divisible by 10
** <div style="margin-left:1.905cm;margin-right:0cm;">Collect the result into a new vector.</div>
+
** Collect the result into a new vector.
** <div style="margin-left:1.905cm;margin-right:0cm;">Finally, print the resulting vector.</div>
+
** Finally, print the resulting vector.
  
 
|| As an assignment,
 
|| As an assignment,
* <div style="margin-left:1.27cm;margin-right:0cm;">Write a Rust program that takes a vector of integers:</div>
+
* Write a Rust program that takes a vector of integers:
* <div style="margin-left:1.27cm;margin-right:0cm;">Using iterators, perform the following steps:</div>
+
* Using iterators, perform the following steps:
 +
 
 +
|- 
 +
|| '''Slide 12'''
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
'''Thank You'''
|| '''Slide 9 Thank You'''
+
 
|| Thanks for joining.
 
|| Thanks for joining.
 
|-
 
|-
 
|}
 
|}
<div style="margin-left:-1.588cm;margin-right:-1.429cm;"></div>
 

Latest revision as of 13:13, 21 October 2025

Visual Cue Narration
Slide 1

Title Slide

Welcome to the Spoken Tutorial on Iterators and Closures in Rust.
Slide 2

Learning Objectives

In this tutorial, we will learn about:
  • Iterators
  • Types of Iterators and
  • Closures
Slide 3

System Requirements To record this tutorial I’m using

To record this tutorial I’m using the following setup.
Slide 4

Code Files

  • iterators.rs
  • The following code file is required to practise this tutorial.
  • This file is provided in the Code Files link of this tutorial page.
Slide 5

Iterators

Let us get started.
  • Iterators are powerful and flexible tools.
  • They process sequences of data one at a time.
  • They help to iterate over a collection of values such as arrays, vectors, maps, etc.
Slide 6 + 7

Type of Iterators

In Rust we can categorize iterators into 3 main types.
  • Basic & Range Iterators
  • Iterator Adapters and
  • Iterator Consumers/Collectors
Let’s learn about Basic and Range Iterators with an example.
Open Visual code editor Open the Visual Studio code editor.
In the menu bar, click on Terminal and select New Terminal. In the menu bar, click on Terminal and select New Terminal.
I have created the filehandling project as explained earlier. Let us go to our working directory MyRustProject as explained earlier.

Type the command cargo new iterators and press Enter.

Open the created project as shown.

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

let numbers = vec![1, 2, 3];

let mut iter = numbers.iter();

println!("{:?}", iter.next()); // Some(1)

println!("{:?}", iter.next()); // Some(2)

println!("{:?}", iter.next()); // Some(3)

println!("{:?}", iter.next()); // None

}

We declared a vector called numbers with values 1, 2, 3.

numbers.iter() method creates an iterator named iter.

iterator can safely access vector elements without looping.

next() method is used to traverse through the items.

It returns a value None when it reaches the end of the collection.

Save the program.

Run the code to see the output.
Let’s see an example of commonly used iterator adapters.

Clear the previous code.

Copy paste the code from the code file.

fn main() {

let numbers = vec![1, 2, 3, 4, 5];

let squared: Vec<i32> = numbers

.iter()

.map(|x| x * x)

.collect();

println!("{:?}", squared); // [1, 4, 9, 16, 25]

}

We declare a vector of integers.

We then create an iterator using the .iter() method.

It gives us immutable references to each element in the vector.

.map() method uses a closure that takes each element x and returns its square.

This doesn't immediately compute the square.

It builds a new iterator pipeline that knows how to compute them when needed.

Finally, .collect() method consumes the iterator and gathers the squares into a new vector

Run the code to see the output.

The result is printed as a new vector [1, 4, 9, 16, 25]

Let’s see an example of Iterator Consumers or Collectors.

These methods consume an iterator and return results like Vec, bool, or a single value.

Copy and paste the code from the code file.
fn main() {

let numbers = vec![1, 2, 3, 4, 5, 6];

let evens: Vec<_> = numbers

.iter()

.filter(|x| *x % 2 == 0)

.collect();


println!("{:?}", evens); // [2, 4, 6]

}

This program shows how to use iterators with filters to show only even numbers from a vector.

Filter() method which takes a closure that returns true or false for each element.

The condition that we have used here will check if the number is even or not.

Only the elements that satisfy this condition are passed forward in the iterator pipeline.

Lastly, .collect() method gathers the remaining elements into new vector evens.

Save the program.

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

The result is [2, 4, 6], it contains only even numbers from the original list.

Slide 8

Closures

  • In Rust, closure is a function without names
  • It can capture variables from its environment, which is a key difference from regular functions.
Next we will see about Closures.
  • Closure is a function without names
  • It can capture variables from its environment, which is a key difference from regular functions.
Let us understand closures with an example.

Switch back to the visual code editor.

Clear the window and copy and paste the code from the codefile.

fn main() {

// define a closure and store it in a variable

let add_one = |x: i32| x + 1;

// call closure and store the result in a variable

let result = add_one(2);

println!("Result = {}", result);

}

Here, parameters are placed between vertical pipelines.

We have defined a closure and binded it to the add_one variable.

We then call the closure with add_one(2) and bind the return value to the result variable.

Closures can omit type annotations and return types, as Rust is capable of type inference

Save the program.

Run the code to see the output.
Let us see another example for multiple statements inside a closure.
Clear the previous code. Copy paste the code from the code file.
fn main() {

// define a multi-line closure

let squared_sum = |x: i32, y: i32| {

// find the sum of two parameters

let mut sum: i32 = x + y;

// find the squared value of the sum

let mut result: i32 = sum * sum;

return result;

};

// call the closure

let result = squared_sum(5, 3);

println!("Result = {}", result);

}

In this example, we enclose the multiple statements using curly braces {}.

First we calculate the sum of two parameters.

Then we find the squared value of the sum.

Save the program.

In the terminal, type cargo run.

We can see the output as 64.

That is the sum of 5 and 3 is 8. The squared value is 64.

Slide 9

Summary

This brings us to the end of this tutorial.

Let us summarize.

Slide 10 + 11

Assignment

  • Write a Rust program that takes a vector of integers:

let numbers = vec![10, 15, 20, 25, 30, 35, 40];

  • Using iterators, perform the following steps:
    • Filter out the numbers that are divisible by 10
    • Collect the result into a new vector.
    • Finally, print the resulting vector.
As an assignment,
  • Write a Rust program that takes a vector of integers:
  • Using iterators, perform the following steps:
Slide 12

Thank You

Thanks for joining.

Contributors and Content Editors

Madhurig, Nirmala Venkat