Difference between revisions of "Rust-Programming-Language/C2/Pattern-Matching/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(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'''
|| <span style="color:#000000;">Welcome to the Spoken Tutorial on </span><span style="color:#000000;">'''Pattern Matching '''</span><span style="color:#000000;">in </span><span style="color:#000000;">'''Rust.'''</span>
+
|| Welcome to the Spoken Tutorial on '''Pattern Matching ''' in '''Rust.'''
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
|| <div style="color:#000000;">Slide 2:</div>
+
||Slide 2:
  
<div style="color:#000000;">Learning Objective</div>
+
Learning Objective
 
|| In this tutorial, we will learn about
 
|| In this tutorial, we will learn about
* <div style="color:#000000;margin-left:1.27cm;margin-right:0cm;">Pattern matching</div>
+
* Pattern matching
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.171cm;padding-right:0.191cm;"
+
|-  
|| <div style="color:#000000;">Slide:</div>
+
||Slide:
  
<div style="color:#000000;">Code Files</div>
+
Code Files
 
||
 
||
* <div style="color:#000000;margin-left:1.27cm;margin-right:0cm;">The following code file is required to practise this tutorial</div>
+
* The following code file is required to practise this tutorial
* <div style="color:#000000;margin-left:1.27cm;margin-right:0cm;">This file is provided in the code files link of this tutorial page</div>
+
* This file is provided in the code files link of this tutorial page
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
|| <div style="color:#000000;">'''Slide:'''</div>
+
||'''Slide:'''
  
<div style="color:#000000;">'''Pattern Matching syntax:'''</div>
+
'''Pattern Matching syntax:'''
  
<div style="color:#000000;">'''match VALUE {'''</div>
+
'''match VALUE {'''
  
<div style="color:#000000;">'''PATTERN => EXPRESSION,'''</div>
+
'''PATTERN => EXPRESSION,'''
  
<div style="color:#000000;">'''PATTERN => EXPRESSION,'''</div>
+
'''PATTERN => EXPRESSION,'''
  
<div style="color:#000000;">'''PATTERN => EXPRESSION,'''</div>
+
'''PATTERN => EXPRESSION,'''
 +
 
 +
'''}'''
  
<div style="color:#000000;">'''}'''</div>
 
 
|| This is the syntax for the pattern matching.
 
|| This is the syntax for the pattern matching.
* <div style="margin-left:1.27cm;margin-right:0cm;">The '''match''' statement is a powerful control flow </div>
+
* The '''match''' statement is a powerful control flow  
* <div style="margin-left:1.27cm;margin-right:0cm;">It allows you to compare a value against a series of patterns </div>
+
* It allows you to compare a value against a series of patterns  
* <div style="margin-left:1.27cm;margin-right:0cm;">It executes the code based on which pattern matches.</div>
+
* It executes the code based on which pattern matches.
* <div style="margin-left:1.27cm;margin-right:0cm;">It is similar to the '''Switch''' statement in other languages.</div>
+
* It is similar to the '''Switch''' statement in other languages.
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
| style="color:#000000;" | Open Visual code editor
+
|| Open Visual code editor
|| <span style="color:#000000;">Let us open the </span><span style="color:#000000;">'''visual code editor'''</span><span style="color:#000000;"> and understand how pattern matching works.</span>
+
|| Let us open the '''visual code editor''' and understand how pattern matching works.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
| style="color:#000000;" | >cd Desktop/MyRustProject
+
|| >cd Desktop/MyRustProject
|| <span style="color:#000000;">Go to our working directory </span><span style="color:#000000;">'''MyRustProject'''</span><span style="color:#000000;"> as explained earlier.</span>
+
|| Go to our working directory '''MyRustProject''' as explained earlier.
  
<span style="color:#000000;">Type the command </span><span style="color:#000000;">'''cargo new </span><span style="color:#000000;">pattern'''</span>
+
Type the command '''cargo new pattern'''
  
<div style="color:#000000;">Open the created project.</div>
+
Open the created project.
  
<span style="background-color:#ffffff;color:#000000;">In the </span><span style="background-color:#ffffff;color:#000000;">'''main.rs '''</span><span style="background-color:#ffffff;color:#000000;">file, replace the </span><span style="background-color:#ffffff;color:#000000;">'''code'''</span><span style="background-color:#ffffff;color:#000000;"> </span><span style="background-color:#ffffff;color:#000000;">as shown.</span>
+
In the '''main.rs '''file, replace the '''code''' as shown.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
|| <div style="color:#252525;">fn main() {</div>
+
|| fn main() {
  
<div style="color:#252525;">let x = 3;</div>
+
let x = 3;
  
<div style="color:#252525;">match x {</div>
+
match x {
  
<div style="color:#252525;">1 => println!("One!"),</div>
+
1 => println!("One!"),
  
<div style="color:#252525;">2 => println!("Two!"),</div>
+
2 => println!("Two!"),
  
<div style="color:#252525;">3 => println!("Three!"),</div>
+
3 => println!("Three!"),
  
<div style="color:#252525;">_ => println!("Something else!"),</div>
+
_ => println!("Something else!"),
  
<div style="color:#252525;">}</div>
+
}
  
<div style="color:#252525;">}</div>
+
}
|| <span style="color:#252525;">The </span><span style="color:#111111;">'''match'''</span><span style="color:#252525;"> </span><span style="color:#252525;">statement is used to compare </span><span style="color:#111111;">'''x</span><span style="color:#252525;"> '''</span><span style="color:#252525;">against different patterns i.e </span><span style="color:#111111;">(1</span><span style="color:#111111;"> or </span><span style="color:#111111;">2</span><span style="color:#111111;"> or </span><span style="color:#111111;">3).</span>
+
|| The'''match''' statement is used to compare'''x '''against different patterns that is 1 or 2 or 3.
  
<span style="color:#252525;">'''underscore'''</span><span style="color:#252525;"> </span><span style="color:#252525;">'''pattern '''</span><span style="color:#252525;">is a </span><span style="color:#252525;">'''catch-all pattern.'''</span>
+
'''underscore''' '''pattern '''is a '''catch-all pattern.'''
<span style="color:#252525;">It</span><span style="color:#252525;"> matches any other value not explicitly covered by previous patterns. </span>
+
  
<span style="color:#111111;">In this case, it will print "Something else!",</span><span style="color:#111111;"> </span><span style="color:#111111;">if x</span><span style="color:#111111;"> </span><span style="color:#111111;">is not 1, 2, or 3. </span>
+
It matches any other value not explicitly covered by previous patterns.  
  
<div style="color:#111111;">Save the code. Let us execute and see the result.</div>
+
In this case, it will print "Something else!", if x is not 1, 2, or 3.  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
| style="color:#000000;" |
+
|| <span style="color:#000000;">In the menu bar, click on terminal and select </span><span style="color:#000000;">'''New Terminal.'''</span>
+
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
Save the code. Let us execute and see the result.
|| <span style="color:#000000;">Type</span><span style="color:#000000;"> </span><span style="color:#000000;">Cargo build</span>
+
|-  
|| <span style="color:#000000;">In the terminal, type </span><span style="color:#000000;">'''cargo run.'''</span>
+
||Click on Terminal and select '''New Terminal'''.
 +
|| In the menu bar, click on Terminal and select '''New Terminal.'''
 +
 
 +
|-
 +
|| Type Cargo build
 +
|| In the terminal, type '''cargo run.'''
  
 
We can see the output as ‘'''Three'''’ which matches the pattern as per the x value.
 
We can see the output as ‘'''Three'''’ which matches the pattern as per the x value.
  
<div style="color:#000000;">Let us change the value of x to 10 in the code.</div>
+
Let us change the value of x to 10 in the code.
  
<div style="color:#000000;"></div>
 
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;" |  
+
||  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;" | <div style="color:#000000;">Save the code and run the program.</div>
+
||Save the code and run the program.
  
<div style="color:#000000;">We can see the output as '''“something else'''” as expected.</div>
+
We can see the output as '''“something else'''” as expected.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;" |  
+
||  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;" | The most common case for pattern matching is with '''Option''' and '''Result''' enum types.
+
|| The most common case for pattern matching is with '''Option''' and '''Result''' enum types.
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;" | <div style="color:#000000;">'''Slide:'''</div>
+
||'''Slide:'''
  
<div style="color:#000000;">'''Matching Option and Result type'''</div>
+
'''Matching Option and Result type'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;" |  
+
|| '''Option''' and '''Result''' type have two variants.
  
<span style="color:#000000;">'''Option'''</span><span style="color:#000000;"> and </span><span style="color:#000000;">'''Result'''</span><span style="color:#000000;"> type have two variants.</span>
+
'''Option''' type has:
  
<div style="color:#000000;">'''Option''' type has:</div>
+
None → It is used to indicate failure with no value
  
<div style="color:#000000;">None →It is used to indicate failure with no value</div>
+
Some(T) → It returns a value with type T
  
<div style="color:#000000;">Some(T) → It returns a value with type T</div>
+
'''Result''' type has:
  
<div style="color:#000000;">'''Result''' type has:</div>
+
Ok(T) → It indicates operation succeeded with value T
  
<div style="color:#000000;">Ok(T) → It indicates operation succeeded with value T</div>
+
Err(E) → It indicates operation failed with an error E
 
+
<div style="color:#000000;">Err(E) → It indicates operation failed with an error E</div>
+
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;color:#000000;" |  
+
||  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;" | <span style="color:#000000;">Let us see an example </span>for<span style="color:#000000;"> </span><span style="color:#000000;">'''Matching Option Type '''</span><span style="color:#000000;">in Rust.</span>
+
|| Let us see an example for '''Matching Option Type '''in Rust.
  
<span style="color:#000000;">Switch back to the </span><span style="color:#000000;">V</span><span style="color:#000000;">isual </span><span style="color:#000000;">Co</span><span style="color:#000000;">de editor.</span>
+
Switch back to the Visual Code editor.
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;" | <div style="color:#000000;">'''fn main() {'''</div>
+
||'''fn main() {'''
  
<div style="color:#000000;">'''fn first_element(array: &[i32]) -> Option<i32> {'''</div>
+
'''fn first_element(array: &[i32]) -> Option<i32> {'''
  
<div style="color:#000000;">'''if array.len() > 0 {'''</div>
+
'''if array.len() > 0 {'''
  
<div style="color:#000000;">'''Some(array[0])'''</div>
+
'''Some(array[0])'''
  
<div style="color:#000000;">'''} else {'''</div>
+
'''} else {'''
  
<div style="color:#000000;">'''None'''</div>
+
'''None'''
  
<div style="color:#000000;">'''}'''</div>
+
'''}'''
  
<div style="color:#000000;">'''}'''</div>
+
'''}'''
  
<div style="color:#000000;">'''let array = [];'''</div>
+
'''let array = [];'''
  
<div style="color:#000000;">'''println!("{:?}", first_element(&array));'''</div>
+
'''println!("{:?}", first_element(&array));'''
  
<div style="color:#000000;">'''}'''</div>
+
'''}'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;" | <div style="color:#000000;">Copy and paste the code from the codefile.</div>
+
||Clear the window and copy and paste the code from the code file.
  
<span style="color:#252525;">Here the </span><span style="color:#252525;">function </span><span style="color:#188038;">first_element</span><span style="color:#252525;">, returns the first element of an integer array if it exists. </span>
+
Here the function '''first_element''', returns the first element of an integer array if it exists.  
  
It takes a reference to an array of integers <span style="color:#c9211e;">(&[i32])</span> as its parameter and returns an <span style="color:#6aa84f;">Option<</span><span style="color:#c9211e;">i32</span><span style="color:#6aa84f;">>.</span>
+
It takes a reference to an array of integers (&[i32]) as its parameter and returns an Option <i32>.
  
<span style="color:#252525;">The function checks the length of the array using </span><span style="color:#188038;">array.len()</span><span style="color:#252525;">. </span>
+
The function checks the length of the array using array.len().  
  
<span style="color:#252525;">If the array has one or more elements, it returns</span><span style="color:#252525;"> </span><span style="color:#188038;">S</span><span style="color:#188038;">ome(array[0])</span><span style="color:#252525;">, i.e </span><span style="color:#252525;">the first element of the array. </span>
+
If the array has one or more elements, it returns Some(array[0]), that is the first element of the array.  
  
<span style="color:#252525;">If the array is empty, it returns </span><span style="color:#188038;">None</span><span style="color:#252525;">, indicating that there is no first element to return.</span>
+
If the array is empty, it returns None, indicating that there is no first element to return.
  
<div style="color:#000000;">Save the file.</div>
+
Save the file.
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;color:#000000;" |  
+
|| Type '''cargo run '''and see the output.
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;" | <div style="color:#000000;">In the terminal, type '''cargo run '''and see the output.</div>
+
||In the terminal, type '''cargo run '''and see the output.
  
<span style="background-color:transparent;color:#000000;">'''None'''</span><span style="background-color:transparent;color:#000000;"> </span><span style="background-color:transparent;color:#000000;">is displayed as output as the array is empty in the program.</span>
+
'''None''' is displayed as output as the array is empty in the program.
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;color:#000000;" |  
+
|| Add 10,20,30 as array elements
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;" | <span style="background-color:transparent;color:#000000;">Let us add 10,20,30 as array elements in the code.</span>
+
|| Let us add 10,20,30 as array elements in the code.
  
<span style="background-color:transparent;color:#000000;">Save the program and run it again.</span>
+
Save the program and run it again.
  
<span style="background-color:transparent;color:#000000;">Now it displays the first element of the array as 10.</span>
+
Now it displays the first element of the array as 10.
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;color:#000000;" |  
+
||  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;" | <span style="color:#000000;">Next we will see </span>about the '''Result<span style="color:#000000;"> enum '''</span><span style="color:#000000;">type.</span>
+
|| Next we will see about the '''Result enum '''type.
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;" | <div style="color:#252525;">fn main() {</div>
+
|| fn main() {
  
<div style="color:#252525;">fn divide(dividend: i32, divisor: i32) -> Result<i32, String> {</div>
+
fn divide(dividend: i32, divisor: i32) -> Result<i32, String> {
  
<div style="color:#252525;">if divisor == 0 {</div>
+
if divisor == 0 {
  
<div style="color:#252525;">Err(String::from("Cannot divide by zero"))</div>
+
Err(String::from("Cannot divide by zero"))
  
<div style="color:#252525;">} else {</div>
+
} else {
  
<div style="color:#252525;">Ok(dividend / divisor)</div>
+
Ok(dividend / divisor)
  
<div style="color:#252525;">}</div>
+
}
  
<div style="color:#252525;">}</div>
+
}
  
<div style="color:#252525;">let result = divide(10, 2);</div>
+
let result = divide(10, 2);
  
<div style="color:#252525;">match result {</div>
+
match result {
  
<div style="color:#252525;">Ok(value) => println!("Result: {}", value),</div>
+
Ok(value) => println!("Result: {}", value),
  
<div style="color:#252525;">Err(error) => println!("Error: {}", error),}</div>
+
Err(error) => println!("Error: {}", error),}
  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;" | <div style="color:#000000;">Copy and paste the code from the codefile.</div>
+
||Clear the window and copy and paste the code from the code file.
  
<div style="color:#252525;">The result enum has two variants representing a correct value and an Error.</div>
+
The result enum has two variants representing a correct value and an Error.
  
<span style="color:#252525;">Here, we define a function </span><span style="color:#188038;">divide</span><span style="color:#252525;"> that performs division.</span>
+
Here, we define a function divide that performs division.
  
<span style="color:#252525;">It returns a </span><span style="color:#188038;">Result</span><span style="color:#252525;"> enum to indicate success or failure.</span>
+
It returns a Result enum to indicate success or failure.
  
<span style="color:#252525;">The </span><span style="color:#188038;">divide</span><span style="color:#252525;"> function takes two parameters: </span><span style="color:#188038;">dividend</span><span style="color:#252525;"> and </span><span style="color:#188038;">divisor</span><span style="color:#252525;">, both of type </span><span style="color:#188038;">i32</span><span style="color:#252525;">. </span>
+
The divide function takes two parameters: dividend and divisor, both of type i32.  
  
<span style="color:#252525;">It returns a </span><span style="color:#188038;">Result<i32, String>. </span>
+
It returns a Result of type <i32.
  
<span style="color:#000000;">It</span><span style="color:#188038;"> </span><span style="color:#000000;">can</span><span style="color:#188038;"> </span><span style="color:#000000;">be</span><span style="color:#252525;"> </span><span style="color:#188038;">Ok </span><span style="color:#252525;">on success i.e (the result of the division).</span>
+
It can be OK on success that is the result of the division.
  
<span style="color:#252525;">Otherwise it returns</span><span style="color:#252525;"> </span><span style="color:#188038;">Err(String)</span><span style="color:#252525;"> if an error occurs i.e(like division by zero).</span>
+
Otherwise it returns Err(String) if an error occurs, that is like division by zero.
  
<span style="color:#000000;">Save th</span>e file.
+
Save the file.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
||  
 
||  
 
|| Run the code and see the output
 
|| Run the code and see the output
  
<span style="background-color:transparent;color:#000000;">It displays 5 as the result of the division.</span>
+
It displays 5 as the result of the division.
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;" |  
+
||  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;" | <span style="background-color:transparent;color:#000000;">Change the parameters in the divide function as (10,0).</span>
+
|| Change the parameters in the divide function as (10,0).
  
<span style="background-color:transparent;color:#000000;">Save the program and run it again.</span>
+
Save the program and run it again.
  
<div style="color:#000000;">It displays the error message.</div>
+
It displays the error message.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
| style="color:#000000;" |  
+
||  
|| <div style="color:#000000;">This brings us to the end of this tutorial.</div>
+
||This brings us to the end of this tutorial.
  
<div style="color:#000000;">Let us summarize. </div>
+
Let us summarize.  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
| style="color:#000000;" |  
+
||  
 
|| In this tutorial, we learn about
 
|| In this tutorial, we learn about
* <div style="margin-left:1.27cm;margin-right:0cm;">Pattern matching</div>
+
* Pattern matching
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
| style="color:#000000;" | Thank You
+
|| Thank You
| style="color:#000000;" | Thanks for joining.
+
|| Thanks for joining.
 
|-
 
|-
 
|}
 
|}
<div style="margin-left:-1.588cm;margin-right:-1.429cm;"></div>
 

Latest revision as of 14:22, 29 August 2025

Visual Cue Narration
Slide 1 Welcome to the Spoken Tutorial on Pattern Matching in Rust.
Slide 2:

Learning Objective

In this tutorial, we will learn about
  • Pattern matching
Slide:

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:

Pattern Matching syntax:

match VALUE {

PATTERN => EXPRESSION,

PATTERN => EXPRESSION,

PATTERN => EXPRESSION,

}

This is the syntax for the pattern matching.
  • The match statement is a powerful control flow
  • It allows you to compare a value against a series of patterns
  • It executes the code based on which pattern matches.
  • It is similar to the Switch statement in other languages.
Open Visual code editor Let us open the visual code editor and understand how pattern matching works.
>cd Desktop/MyRustProject Go to our working directory MyRustProject as explained earlier.

Type the command cargo new pattern

Open the created project.

In the main.rs file, replace the code as shown.

fn main() {

let x = 3;

match x {

1 => println!("One!"),

2 => println!("Two!"),

3 => println!("Three!"),

_ => println!("Something else!"),

}

}

Thematch statement is used to comparex against different patterns that is 1 or 2 or 3.

underscore pattern is a catch-all pattern.

It matches any other value not explicitly covered by previous patterns.

In this case, it will print "Something else!", if x is not 1, 2, or 3.

Save the code. Let us execute and see the result.

Click on Terminal and select New Terminal. In the menu bar, click on Terminal and select New Terminal.
Type Cargo build In the terminal, type cargo run.

We can see the output as ‘Three’ which matches the pattern as per the x value.

Let us change the value of x to 10 in the code.

Save the code and run the program.

We can see the output as “something else” as expected.

The most common case for pattern matching is with Option and Result enum types.
Slide:

Matching Option and Result type

Option and Result type have two variants.

Option type has:

None → It is used to indicate failure with no value

Some(T) → It returns a value with type T

Result type has:

Ok(T) → It indicates operation succeeded with value T

Err(E) → It indicates operation failed with an error E

Let us see an example for Matching Option Type in Rust.

Switch back to the Visual Code editor.

fn main() {

fn first_element(array: &[i32]) -> Option<i32> {

if array.len() > 0 {

Some(array[0])

} else {

None

}

}

let array = [];

println!("{:?}", first_element(&array));

}

Clear the window and copy and paste the code from the code file.

Here the function first_element, returns the first element of an integer array if it exists.

It takes a reference to an array of integers (&[i32]) as its parameter and returns an Option <i32>.

The function checks the length of the array using array.len().

If the array has one or more elements, it returns Some(array[0]), that is the first element of the array.

If the array is empty, it returns None, indicating that there is no first element to return.

Save the file.

Type cargo run and see the output. In the terminal, type cargo run and see the output.

None is displayed as output as the array is empty in the program.

Add 10,20,30 as array elements Let us add 10,20,30 as array elements in the code.

Save the program and run it again.

Now it displays the first element of the array as 10.

Next we will see about the Result enum type.
fn main() {

fn divide(dividend: i32, divisor: i32) -> Result<i32, String> {

if divisor == 0 {

Err(String::from("Cannot divide by zero"))

} else {

Ok(dividend / divisor)

}

}

let result = divide(10, 2);

match result {

Ok(value) => println!("Result: {}", value),

Err(error) => println!("Error: {}", error),}

Clear the window and copy and paste the code from the code file.

The result enum has two variants representing a correct value and an Error.

Here, we define a function divide that performs division.

It returns a Result enum to indicate success or failure.

The divide function takes two parameters: dividend and divisor, both of type i32.

It returns a Result of type <i32.

It can be OK on success that is the result of the division.

Otherwise it returns Err(String) if an error occurs, that is like division by zero.

Save the file.

Run the code and see the output

It displays 5 as the result of the division.

Change the parameters in the divide function as (10,0).

Save the program and run it again.

It displays the error message.

This brings us to the end of this tutorial.

Let us summarize.

In this tutorial, we learn about
  • Pattern matching
Thank You Thanks for joining.

Contributors and Content Editors

Madhurig, Nirmala Venkat