Rust-Programming-Language/C2/Pattern-Types/English

From Script | Spoken-Tutorial
Jump to: navigation, search
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,
  • Irrefutable pattern and
  • Refutable pattern
Slide 3

System Requirements

This tutorial is recorded using,
  • Ubuntu Linux OS version 22.04
  • Rust version 1.80.0
  • Visual studio code version 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

pattern.rs

  • The following code file is required to practise this tutorial.
  • This file is provided in the Code files link of this tutorial page.
Show slide:

Irrefutable patterns

  • Irrefutable patterns always match at the declaration.
  • They are used in places where failure to match is not allowed.

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
  • Refutable patterns
Next type is Refutable patterns
  • These patterns may fail to match.
  • They are used where the compiler expects the possibility of failure.

Used in: if let, while let, match statements

Slide:

If let – While let

  • if let / while let expression is a shorthand for a match expression with only one pattern to match.
  • It allows us to match on a value and then execute code only if the match is successful
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.
  • Analyze the compilation error.
  • Use If let and correct the code.
  • Run the program.
Slide 11

Thank You

Thanks for joining.

Contributors and Content Editors

Madhurig, Nirmala Venkat