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...") |
|||
| Line 6: | Line 6: | ||
|| '''Slide 1''' | || '''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'''. | || In the menu bar, click on '''Terminal''' and select''' New Terminal'''. | ||
| − | + | |- | |
| − | + | ||
| − | + | ||
| − | |- | + | |
|| '''> 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. | ||
| − | |- | + | |- |
|| 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. |
| − | |- | + | |- |
| − | || Pub | + | || 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 <nowiki>=> println!("Move Left"),</nowiki> | |
| − | + | Direction::Right <nowiki>=> println!("Move Right"),</nowiki> | |
| − | + | } | |
| − | + | } | |
| − | + | fn main() { | |
| − | + | let dir = Direction::Left; | |
| − | + | move_direction(dir); | |
| − | + | } | |
| − | 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'''. | ||
| − | + | '''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.''' | || 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. | ||
| − | |- | + | |- |
|| 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. | ||
| − | |- | + | |- |
| − | || | + | || 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 ''' | + | '''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() ''' | + | 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. | The range values are assigned to red, yellow and green. | ||
| − | '''match''' | + | '''match''' statement is used to print the current color of the traffic light along with the duration. |
| − | After printing | + | After printing the traffic light status, the program pauses for 1 second i.e (1000 milliseconds) . |
| − | + | Then the loop continues. | |
Save the program. | Save the program. | ||
| − | |- | + | |- |
|| In the terminal, type '''cargo run''' | || In the terminal, type '''cargo run''' | ||
press '''Ctrl''' and''' C''' keys | press '''Ctrl''' and''' C''' keys | ||
| − | || | + | || 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. | ||
| − | + | To terminate the program, press '''Ctrl''' and''' C''' keys together. | |
| − | |- | + | |- |
|| '''Slide 7''' | || '''Slide 7''' | ||
| − | + | '''Summary''' | |
In this tutorial, we learn about | In this tutorial, we learn about | ||
| − | + | '''Enums''' | |
| − | || | + | || This brings us to the end of this tutorial. |
| − | + | Let us summarize. | |
| − | |- | + | |- |
|| '''Slide 8''' | || '''Slide 8''' | ||
| Line 238: | Line 241: | ||
|| As an Assignment, do the following: | || 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''' | || '''Slide 9''' | ||
| − | + | '''Thank You''' | |
| − | || | + | || Thank you for joining. |
|- | |- | ||
|} | |} | ||
| − | |||
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:
|
| 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 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. |