Difference between revisions of "Rust-Programming-Language/C2/Enums/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;">'''Enums in Rust.'''</span>
 
|- style="background-color:transparent;border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"
 
|| <span style="color:#000000;">'''Slide 2'''</span>
 
  
<span style="color:#000000;">'''Learning Objectives'''</span>
+
'''Title Slide'''
|| <span style="color:#000000;">In this tutorial, we will learn about:</span>
+
|| Welcome to the Spoken Tutorial on '''Enums in Rust.'''
* <div style="color:#000000;margin-left:1.27cm;margin-right:0cm;">Enums</div>
+
|-  
 +
|| '''Slide 2'''
  
|- style="background-color:transparent;border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.136cm;padding-right:0.191cm;"
+
'''Learning Objectives'''
|| <span style="color:#000000;">'''Slide </span>3'''
+
|| In this tutorial, we will learn about:
 +
* Enums
  
<span style="color:#000000;">'''System Requirements'''</span>
+
|-
|| <span style="color:#000000;">This tutorial</span> is recorded<span style="color:#000000;"> using,</span>
+
|| '''Slide 3'''
* <div style="margin-left:1.27cm;margin-right:0cm;"><span style="color:#000000;">Ubuntu Linux OS version 22.04</span></div>
+
* <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>
+
* <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>
+
  
|- style="background-color:transparent;border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.136cm;padding-right:0.191cm;"
+
'''System Requirements'''
|| <span style="color:#000000;">'''Slide </span>4'''
+
|| This tutorial is recorded using,
 +
* Ubuntu Linux OS version 22.04
 +
* Rust 1.80.0
 +
* Visual Studio Code 1.19.0
  
<span style="color:#000000;">'''Prerequisites'''</span>
+
|-  
|| To follow this tutorial, y<span style="color:#000000;">ou should be familiar with compiling and running a Rust </span>program<span style="color:#000000;">.</span>
+
|| '''Slide 4'''
|- style="background-color:transparent;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'''
+
  
<span style="color:#000000;">'''Code Files'''</span>
+
'''Prerequisites'''
 +
|| To follow this tutorial, you should be familiar with compiling and running a Rust program.
 +
|-
 +
|| '''Slide 5'''
 +
 
 +
'''Code Files'''
 
||
 
||
* <div style="color:#000000;margin-left:1.27cm;margin-right:0cm;"><span style="background-color:transparent;">The following code file is required to practise this tutorial.</span></div>
+
* The following code file is required to practise this tutorial.
* <div style="color:#000000;margin-left:1.27cm;margin-right:0cm;"><span style="background-color:transparent;">This file is provided in the </span>C<span style="background-color:transparent;">ode </span>F<span style="background-color:transparent;">iles link of this tutorial page.</span></div>
+
* This file is provided in the Code Files link of this tutorial page.
  
|- style="background-color:transparent;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'''
  
<span style="color:#000000;">'''Enum'''</span>
+
'''Enums'''
 
||
 
||
* <div style="color:#000000;margin-left:1.27cm;margin-right:0cm;">Enums (or enumerations) is a user-defined data type.</div>
+
* Enums (or enumerations) is a user-defined data type.
* <div style="color:#000000;margin-left:1.27cm;margin-right:0cm;">It allows us to select a value from a list of related values.</div>
+
* It allows us to select a value from a list of related values.
* <div style="color:#000000;margin-left:1.27cm;margin-right:0cm;">An enumerated type is declared using the '''enum''' keyword.</div>
+
* An enumerated type is declared using the '''enum''' keyword.
  
|- style="background-color:transparent;border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.136cm;padding-right:0.191cm;"
+
|-  
| style="color:#000000;" |  
+
||  
|| <span style="color:#000000;">Let us understand about </span><span style="color:#000000;">'''enums'''</span><span style="color:#000000;"> using a</span><span style="color:#000000;">''' rust'''</span><span style="color:#000000;"> program.</span>
+
|| Let us understand about '''enums''' using a''' rust''' program.
|- style="background-color:transparent;border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;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;">Open the </span>'''V<span style="color:#000000;">isual studio code editor.'''</span>
+
|| Open the '''Visual studio code editor.'''
|- style="background-color:transparent;border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;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><span style="color:#000000;">.</span>
+
|-  
 
+
<div style="color:#000000;"></div>
+
|- style="background-color:transparent;border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"
+
 
|| '''> cd''' '''Desktop'''/'''MyRustProject'''
 
|| '''> cd''' '''Desktop'''/'''MyRustProject'''
  
 
'''>''' '''cargo new enumdemo'''
 
'''>''' '''cargo new enumdemo'''
'''In the menu bar, File >> Open folder >> Desktop >> MyRustProject >> enumdemo'''
+
 
 +
'''In the menu bar, File >> Open folder
 +
 
 +
>> Desktop >> MyRustProject >> enumdemo'''
 
|| Let us go to our working directory '''MyRustProject''' as explained earlier.
 
|| Let us go to our working directory '''MyRustProject''' as explained earlier.
  
 
Type the command '''cargo new enumdemo '''and press '''Enter.'''
 
Type the command '''cargo new enumdemo '''and press '''Enter.'''
 
Open the created project as shown.
 
Open the created project as shown.
|- style="background-color:transparent;border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"
+
|-  
 
|| Point to the '''main.rs''' file.
 
|| Point to the '''main.rs''' file.
  
| style="color:#000000;" | <span style="background-color:#ffffff;">In the </span><span style="background-color:#ffffff;">'''main.rs '''</span><span style="background-color:#ffffff;">file, </span><span style="background-color:#ffffff;">copy and paste the code from the Code file.</span>
+
|| In the '''main.rs '''file, copy and paste the code from the Code file.
|- style="background-color:transparent;border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"
+
|-  
|| Pub <span style="color:#000000;">enum Direction {</span>
+
|| Pub enum Direction {
  
<span style="color:#000000;">Up,</span>
+
Up,
  
<span style="color:#000000;">Down,</span>
+
Down,
  
<span style="color:#000000;">Left,</span>
+
Left,
  
<span style="color:#000000;">Right,</span>
+
Right,
  
<span style="color:#000000;">}</span>
+
}
  
<span style="color:#000000;">fn move_direction(dir: Direction) {</span>
+
fn move_direction(dir: Direction) {
  
<span style="color:#000000;">match dir {</span>
+
match dir {
  
<span style="color:#000000;">Direction::Up => println!("Move Up"),</span>
+
Direction::Up => println!("Move Up"),
  
<span style="color:#000000;">Direction::Down => println!("Move Down"),</span>
+
Direction::Down => println!("Move Down"),
  
<span style="color:#000000;">Direction::Left <nowiki>=> println!("Move Left"),</nowiki></span>
+
Direction::Left <nowiki>=> println!("Move Left"),</nowiki>
  
<span style="color:#000000;">Direction::Right <nowiki>=> println!("Move Right"),</nowiki></span>
+
Direction::Right <nowiki>=> println!("Move Right"),</nowiki>
  
<span style="color:#000000;">}</span>
+
}
  
<span style="color:#000000;">}</span>
+
}
  
<span style="color:#000000;">fn main() {</span>
+
fn main() {
  
<span style="color:#000000;">let dir = Direction::Left;</span>
+
let dir = Direction::Left;
  
<span style="color:#000000;">move_direction(dir);</span>
+
move_direction(dir);
  
<span style="color:#000000;">}</span>
+
}
  
Press Ctrl+S
+
Press Ctrl + S
 
|| Here''' Direction '''is an '''enum''' with four possible values.  
 
|| Here''' Direction '''is an '''enum''' with four possible values.  
  
Line 114: Line 117:
 
These enum values are known as '''variants'''.
 
These enum values are known as '''variants'''.
  
<span style="color:#000000;">'''Direction'''</span><span style="color:#000000;">::</span><span style="color:#000000;">'''Up'''</span><span style="color:#000000;"> represents </span><span style="color:#000000;">'''enum variant'''</span><span style="color:#000000;"> </span><span style="color:#000000;">'''Up'''</span><span style="color:#000000;"> of the </span><span style="color:#000000;">'''Direction'''</span><span style="color:#000000;"> enum</span>
+
'''Direction'''::'''Up''' represents '''enum variant''' '''Up''' of the '''Direction''' enum
  
<div style="color:#000000;">W<span style="background-color:transparent;">e can access </span>t<span style="background-color:transparent;">he </span><span style="background-color:transparent;">'''enum'''</span><span style="background-color:transparent;"> </span><span style="background-color:transparent;">variants whenever we have to use a </span><span style="background-color:transparent;">'''direction'''</span><span style="background-color:transparent;"> in our program.</span></div>
+
We can access the '''enum''' variants whenever we have to use a '''direction''' in our program.
  
<span style="background-color:transparent;color:#000000;">In the</span><span style="background-color:transparent;color:#000000;">''' main '''</span><span style="background-color:transparent;color:#000000;">function, we are </span>accessing the<span style="background-color:transparent;color:#000000;"> </span><span style="background-color:transparent;color:#000000;">'''Left'''</span><span style="background-color:transparent;color:#000000;">” variant.</span>
+
In the''' main '''function, we are accessing the “'''Left'''” variant.
  
<span style="background-color:transparent;color:#000000;">Save the program. Let us execute the code and see the output.</span>
+
Save the program. Let us execute the code and see the output.
|- style="background-color:transparent;border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"
+
|-  
 
|| Click on terminal and select '''New Terminal.'''
 
|| Click on terminal and select '''New Terminal.'''
  
Line 132: Line 135:
  
 
It prints “'''Move Left'''” as the output.
 
It prints “'''Move Left'''” as the output.
|- style="background-color:transparent;border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"
+
|-  
 
|| Delete the existing code.
 
|| Delete the existing code.
 
|| Next we will see another example for '''enum'''.  
 
|| Next we will see another example for '''enum'''.  
  
 
Clear the window and copy and paste the code from the Code file.
 
Clear the window and copy and paste the code from the Code file.
|- style="background-color:transparent;border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"
+
|-  
|| <span style="color:#000000;">use std::thread::sleep;</span>
+
|| use std::thread::sleep;
  
<span style="color:#000000;">use std::time::Duration;</span>
+
use std::time::Duration;
  
<span style="color:#000000;">enum TrafficLight {</span>
+
enum TrafficLight {
  
<span style="color:#000000;">Red,</span>
+
Red,
  
<span style="color:#000000;">Yellow,</span>
+
Yellow,
  
<span style="color:#000000;">Green,</span>
+
Green,
  
<span style="color:#000000;">}</span>
+
}
  
<span style="color:#000000;">fn main() {</span>
+
fn main() {
  
<span style="color:#000000;">loop {</span>
+
loop {
  
<span style="color:#000000;">for item in (1..=28).rev() {</span>
+
for item in (1..=28).rev() {
  
<span style="color:#000000;">let now = match item {</span>
+
let now = match item {
  
<span style="color:#000000;">1..=15 => TrafficLight::Red,</span>
+
1..=15 => TrafficLight::Red,
  
<span style="color:#000000;">16..=18 => TrafficLight::Yellow,</span>
+
16..=18 => TrafficLight::Yellow,
  
<span style="color:#000000;">19..=28 => TrafficLight::Green,</span>
+
19..=28 => TrafficLight::Green,
  
<span style="color:#000000;">_ => TrafficLight::Red,</span>
+
_ => TrafficLight::Red,
  
<span style="color:#000000;">};</span>
+
};
  
<span style="color:#000000;">match now {</span>
+
match now {
  
<span style="color:#000000;">TrafficLight::Red => println!("🔴 </span><span style="color:#000000;">Duration: {}", item),</span>
+
TrafficLight::Red => println!("🔴 Duration: {}", item),
  
<span style="color:#000000;">TrafficLight::Yellow => println!("🟡 Duration: {}", item - 15),</span>
+
TrafficLight::Yellow => println!("🟡 Duration: {}", item - 15),
  
<span style="color:#000000;">TrafficLight::Green => println!("🟢 Duration: {}", item – 18),</span>
+
TrafficLight::Green => println!("🟢 Duration: {}", item – 18),
  
<span style="color:#000000;">} sleep(Duration::from_millis(1000));</span>
+
} sleep(Duration::from_millis(1000));
  
<span style="color:#000000;">}</span>
+
}
  
<span style="color:#000000;">}</span>
+
}
  
<span style="color:#000000;">}</span>Ctrl+S
+
}Ctrl + S
 
||  
 
||  
  
<span style="color:#000000;">This code simulates a traffic light system which changes colors at specified intervals.</span>
+
This code simulates a traffic light system which changes colors at specified intervals.
  
<span style="color:#000000;">'''TrafficLight enum'''</span><span style="color:#000000;"> has 3 </span>variants<span style="color:#000000;"> as Red, Yellow and Green.</span>
+
'''TrafficLight enum''' has 3 variants as Red, Yellow and Green.
  
<span style="color:#000000;">The code imports the </span>'''sleep'''<span style="color:#000000;"> function from the</span>''' std:: thread '''<span style="color:#000000;">module.</span>
+
The code imports the '''sleep''' function from the''' std:: thread '''module.
  
'''Duration struct '''<span style="color:#000000;">from the</span>''' std::time '''<span style="color:#000000;">module is used to create pauses in the loop.</span>
+
'''Duration struct '''from the''' std::time '''module is used to create pauses in the loop.
  
<span style="color:#000000;">The </span>'''main'''<span style="color:#000000;"> function begins with an infinite loop.</span>
+
The '''main''' function begins with an infinite loop.
  
<span style="color:#000000;">The </span>'''for'''<span style="color:#000000;"> loop iterates over the range from</span>''' '''1 to 28,<span style="color:#000000;"> </span><span style="color:#000000;">in the reverse order.</span>
+
The '''for''' loop iterates over the range from''' '''1 to 28, in the reverse order.
  
Note that''' .rev() '''<span style="color:#000000;">means reverse order</span>.
+
Note that''' .rev() '''means reverse order.
  
<span style="color:#000000;">The </span>'''match'''<span style="color:#000000;"> statement assigns a value to the variable </span>'''now'''<span style="color:#000000;"> based on the current </span>'''item'''<span style="color:#000000;"> value</span>.
+
The '''match''' statement assigns a value to the variable '''now''' based on the current '''item''' value.
  
 
The range values are assigned to red, yellow and green.
 
The range values are assigned to red, yellow and green.
  
'''match'''<span style="color:#000000;"> statement is used to print the current color of the traffic light along with the duration.</span>
+
'''match''' statement is used to print the current color of the traffic light along with the duration.
  
After printing <span style="color:#000000;">the traffic light status, the program pauses for 1 second i.e (1000 milliseconds) .</span>
+
After printing the traffic light status, the program pauses for 1 second i.e (1000 milliseconds) .
  
<span style="color:#000000;">Then the loop </span>continues<span style="color:#000000;">.</span>
+
Then the loop continues.
  
 
Save the program.
 
Save the program.
|- style="background-color:transparent;border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"
+
|-  
 
|| In the terminal, type '''cargo run'''
 
|| In the terminal, type '''cargo run'''
  
 
press '''Ctrl''' and''' C''' keys  
 
press '''Ctrl''' and''' C''' keys  
|| <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 that traffic light color and its duration running continuously.
 
We can see that traffic light color and its duration running continuously.
  
<span style="color:#000000;">To terminate the program, press </span><span style="color:#000000;">'''Ctrl'''</span><span style="color:#000000;"> and</span><span style="color:#000000;">''' C'''</span><span style="color:#000000;"> keys together.</span>
+
To terminate the program, press '''Ctrl''' and''' C''' keys together.
|- style="background-color:transparent;border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"
+
|-  
 
|| '''Slide 7'''
 
|| '''Slide 7'''
  
<span style="color:#000000;">'''Summary'''</span>
+
'''Summary'''
  
 
In this tutorial, we learn about
 
In this tutorial, we learn about
  
<span style="color:#000000;">'''Enums'''</span>
+
'''Enums'''
|| <span style="color:#000000;">This brings us to the end of this tutorial.</span>
+
|| This brings us to the end of this tutorial.
  
<span style="color:#000000;">Let us summarize. </span>
+
Let us summarize.  
|- style="background-color:transparent;border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"
+
|-  
 
|| '''Slide 8'''
 
|| '''Slide 8'''
  
Line 238: Line 241:
 
|| As an Assignment, do the following:
 
|| As an Assignment, do the following:
  
* <div style="margin-left:1.27cm;margin-right:0cm;">Define an '''enum''' '''Operation''' with variants: '''Add, <span style="color:#000000;">Subtract</span>, Multiply, and Divide.'''</div>
+
* Define an '''enum''' '''Operation''' with variants: '''Add, Subtract, Multiply, and Divide.'''
* <div style="margin-left:1.27cm;margin-right:0cm;">Write a function '''calculate''' with two f64 numbers and one '''Operation'''</div>
+
* Write a function '''calculate''' with two f64 numbers and one '''Operation'''
* <div style="margin-left:1.27cm;margin-right:0cm;">Function returns the f64 result.</div>
+
* Function returns the f64 result.
* <div style="margin-left:1.27cm;margin-right:0cm;">In the main function, call '''calculate''' with different operations and print the results.</div>
+
* In the main function, call '''calculate''' with different operations and print the results.
  
|- style="background-color:transparent;border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"
+
|-  
 
|| '''Slide 9'''
 
|| '''Slide 9'''
  
<span style="color:#000000;">'''Thank You'''</span>
+
'''Thank You'''
|| <span style="color:#000000;">Thank you for joining.</span>
+
|| Thank you for joining.
  
 
|-
 
|-
 
|}
 
|}
<div style="margin-left:-1.588cm;margin-right:-1.429cm;"></div>
 

Revision as of 17:27, 21 August 2025

Visual Cue Narration
Slide 1

Title Slide

Welcome to the Spoken Tutorial on Enums in Rust.
Slide 2

Learning Objectives

In this tutorial, we will learn about:
  • Enums
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.
Slide 6

Enums

  • Enums (or enumerations) is a user-defined data type.
  • It allows us to select a value from a list of related values.
  • An enumerated type is declared using the enum keyword.
Let us understand about enums using a rust program.
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.
> cd Desktop/MyRustProject

> cargo new enumdemo

In the menu bar, File >> Open folder

>> Desktop >> MyRustProject >> enumdemo

Let us go to our working directory MyRustProject as explained earlier.

Type the command cargo new enumdemo 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.
Pub enum Direction {

Up,

Down,

Left,

Right,

}

fn move_direction(dir: Direction) {

match dir {

Direction::Up => println!("Move Up"),

Direction::Down => println!("Move Down"),

Direction::Left => println!("Move Left"),

Direction::Right => println!("Move Right"),

}

}

fn main() {

let dir = Direction::Left;

move_direction(dir);

}

Press Ctrl + S

Here Direction is an enum with four possible values.

They are: up, down, left, and right.

These enum values are known as variants.

Direction::Up represents enum variant Up of the Direction enum

We can access the enum variants whenever we have to use a direction in our program.

In the main function, we are accessing the “Left” variant.

Save the program. Let us execute the code and see the output.

Click on terminal and select New Terminal.

In the terminal, type cargo run.

Point to the output.

In the menu bar, click on Terminal and select New Terminal.

In the terminal, type cargo run.

It prints “Move Left” as the output.

Delete the existing code. Next we will see another example for enum.

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

use std::thread::sleep;

use std::time::Duration;

enum TrafficLight {

Red,

Yellow,

Green,

}

fn main() {

loop {

for item in (1..=28).rev() {

let now = match item {

1..=15 => TrafficLight::Red,

16..=18 => TrafficLight::Yellow,

19..=28 => TrafficLight::Green,

_ => TrafficLight::Red,

};

match now {

TrafficLight::Red => println!("🔴 Duration: {}", item),

TrafficLight::Yellow => println!("🟡 Duration: {}", item - 15),

TrafficLight::Green => println!("🟢 Duration: {}", item – 18),

} sleep(Duration::from_millis(1000));

}

}

}Ctrl + S

This code simulates a traffic light system which changes colors at specified intervals.

TrafficLight enum has 3 variants as Red, Yellow and Green.

The code imports the sleep function from the std:: thread module.

Duration struct from the std::time module is used to create pauses in the loop.

The main function begins with an infinite loop.

The for loop iterates over the range from 1 to 28, in the reverse order.

Note that .rev() means reverse order.

The match statement assigns a value to the variable now based on the current item value.

The range values are assigned to red, yellow and green.

match statement is used to print the current color of the traffic light along with the duration.

After printing the traffic light status, the program pauses for 1 second i.e (1000 milliseconds) .

Then the loop continues.

Save the program.

In the terminal, type cargo run

press Ctrl and C keys

In the terminal, type cargo run

We can see that traffic light color and its duration running continuously.

To terminate the program, press Ctrl and C keys together.

Slide 7

Summary

In this tutorial, we learn about

Enums

This brings us to the end of this tutorial.

Let us summarize.

Slide 8

Assignment

As an Assignment, do the following:
  • Define an enum Operation with variants: Add, Subtract, Multiply, and Divide.
  • Write a function calculate with two f64 numbers and one Operation
  • Function returns the f64 result.
  • In the main function, call calculate with different operations and print the results.
Slide 9

Thank You

Thank you for joining.

Contributors and Content Editors

Madhurig, Nirmala Venkat