Rust-Programming-Language/C2/Pattern-Types/English
| Visual Cue | Narration |
| Slide 1 | Welcome to the Spoken Tutorial on Pattern Types 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 pattern.rs |
|
| Show slide:
Irrefutable patterns |
For example, the variable x is assigned directly a value 5. Similarly y and z are declared with the values 3 and 4. We are binding them directly, and they don’t have any other form of declaration. So it is irrefutable. |
Slide 8
|
Next type is Refutable patterns
Used in: if let, while let, match statements |
| Slide:
If let – While let |
|
| Next let us see an example for if let expression in Rust. | |
| Switch back to the Visual Code editor.
Copy paste the code from the Code file. |
In the visual code Editor, copy and paste the code from the codefile. |
| fn main() {
let authorisation_status = Some("approved"); if let Some(status) = authorisation_status { println!("Authorisation status: {status}"); } } |
We define a variable authorisation_status and assign it an Option value of Some("approved").
Now, look closely at the second line of the code. Here we used the if let keyword to define the refutable pattern match Some(status). It checks for two values. If authorisation_status is Some(), then it unwraps the value into the variable status. If it's None, the block is skipped. That is the code is not executed. Since authorisation_status is Some("approved"), it matches. The string "approved" is assigned to the variable Save the program. |
| Type cargo run to run the code.
Approved is shown as the output. |
Run the code.
The output is shown as Authorisation status: approved |
| Now let us see about, while let expression in Rust. | |
| Copy paste the code from the Code file. | Copy and paste the code from the Code file |
| fn main() {
let mut number = Some(5); while let Some(count) = number { println!("Countdown: {}", count); if count == 1 { number = None; // Stop when it reaches 1 } else { number = Some(count - 1); // Decrease the count } } println!("Liftoff!"); } |
First we defined a mutable variable called number which is of the type Option enum.
The variable number starts as Some(5). while let Some(count) is a refutable pattern. When pattern matching happens at the while let statement, Some(count) tries to match the value. Each loop iteration decreases, number by 1. When the count reaches 1, it sets number = None, ending the loop. while let is used when you want to loop only as long as the pattern matches. Save the file. |
| Type cargo run to run the code. | Run the code to see the output.
We can see that Countdown stops when it reaches 1 and prints Liftoff! |
| This brings us to the end of this tutorial.
Let us summarize. | |
| Slide 10
Assignment fn main() { let Some(x) = Some(10); println!("x = {}", x); } |
As an assignment, execute the below program.
|
| Slide 11
Thank You |
Thanks for joining. |