Difference between revisions of "Rust-Programming-Language/C2/Enums/English"
| Line 8: | Line 8: | ||
'''Title Slide''' | '''Title Slide''' | ||
| − | || Welcome to the Spoken Tutorial on '''Enums in Rust | + | || Welcome to the Spoken Tutorial on '''Enums in Rust '''. |
|- | |- | ||
|| '''Slide 2''' | || '''Slide 2''' | ||
| Line 36: | Line 36: | ||
|| | || | ||
* The following code file is required to practise this tutorial. | * The following code file is required to practise this tutorial. | ||
| − | * This file is provided in the Code Files link of this tutorial page. | + | * This file is provided in the '''Code Files''' link of this tutorial page. |
|- | |- | ||
| Line 43: | Line 43: | ||
'''Enums''' | '''Enums''' | ||
|| | || | ||
| − | * Enums (or enumerations) is a user-defined data type. | + | * '''Enums''' (or enumerations) is a user-defined data type. |
* It allows us to select a value from a list of related values. | * It allows us to select a value from a list of related values. | ||
* An enumerated type is declared using the '''enum''' keyword. | * An enumerated type is declared using the '''enum''' keyword. | ||
| Line 52: | Line 52: | ||
|- | |- | ||
|| Open '''Visual''' '''code editor''' | || Open '''Visual''' '''code editor''' | ||
| − | || Open the '''Visual | + | || 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'''. | ||
| Line 68: | Line 68: | ||
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. | ||
|- | |- | ||
|| 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. | + | || In the '''main.rs ''' file, copy and paste the code from the Code file. |
|- | |- | ||
|| Pub enum Direction { | || Pub enum Direction { | ||
| Line 111: | Line 112: | ||
Press Ctrl + S | Press Ctrl + S | ||
| − | || Here''' Direction '''is an '''enum''' with four possible values. | + | || Here''' Direction ''' is an '''enum''' with four possible values. |
They are: up, down, left, and right. | They are: up, down, left, and right. | ||
| − | These enum values are known as '''variants'''. | + | These '''enum''' values are known as '''variants'''. |
| − | '''Direction'''::'''Up''' represents '''enum variant | + | '''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. | 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. | + | In the ''' main '''function, we are accessing the “'''Left'''” variant. |
Save the program. Let us execute the code and see the output. | Save the program. Let us execute the code and see the output. | ||
| Line 188: | Line 189: | ||
}Ctrl + S | }Ctrl + S | ||
| − | || | + | || This code simulates a traffic light system which changes colors at specified intervals. |
| − | + | ||
| − | This code simulates a traffic light system which changes colors at specified intervals. | + | |
'''TrafficLight enum''' has 3 variants as Red, Yellow and Green. | '''TrafficLight enum''' has 3 variants as Red, Yellow and Green. | ||
| Line 200: | Line 199: | ||
The '''main''' function begins with an infinite loop. | The '''main''' function begins with an infinite loop. | ||
| − | The '''for''' loop iterates over the range from | + | The '''for''' loop iterates over the range from 1 to 28, in the reverse order. |
Note that''' .rev() '''means reverse order. | Note that''' .rev() '''means reverse order. | ||
| Line 210: | Line 209: | ||
'''match''' statement is used to print the current color of the traffic light along with the duration. | '''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 | + | After printing the traffic light status, the program pauses for 1 second that is (1000 milliseconds) . |
Then the loop continues. | Then the loop continues. | ||
| Line 223: | Line 222: | ||
We can see that traffic light color and its duration running continuously. | We can see that traffic light color and its duration running continuously. | ||
| − | To terminate the program, press '''Ctrl''' and''' C''' keys together. | + | To terminate the program, press '''Ctrl''' and ''' C''' keys together. |
|- | |- | ||
|| '''Slide 7''' | || '''Slide 7''' | ||
Revision as of 17:57, 25 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:
|
| Slide 3
System Requirements |
This tutorial is recorded using,
|
| Slide 4
Prerequisites |
To follow this tutorial, you should be familiar with compiling and running a Rust program. |
| Slide 5
Code Files |
|
| Slide 6
Enums |
|
| 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 that is (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:
|
| Slide 9
Thank You |
Thank you for joining. |