Difference between revisions of "Rust-Programming-Language/C2/Enums/English"
(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...") |
(No difference)
|
Revision as of 14:44, 18 August 2025
Visual Cue | Narration |
Slide 1 | 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
Enum |
|
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:
|
Slide 9
Thank You |
Thank you for joining. |