Welcome our friends in this video we will be going to discuss our topic that is all the places where patterns can be used patterns pop up in number of places and rust and you have been using them a lot without realizing it the section provides you with a reference to all the places where patterns are valid first we.
Discussing about the mare charms so we have used patterns in the arms of match expression formerly match expression are defined as the keyword match a value to.
One or more match arms that consist of a pattern and an expression to run if the value matches that arms pattern so how our match pattern works so I am showing an.
Example so our match pattern looks something like this we write match then we give a value to it and then inside it what we have simply we give a pattern and then after pattern we give expression so one requirement of match expression is that they need to be exhaustive in the sense.
That all possibilities for the value in the match expression must be accounted for one way to ensure you have covered every possibility is to have a cash fall pattern for the last amp so a.
Particular pattern that is this so this will match to anything but it never binds to a variable so it’s often used in last match arm the underscore pattern can be useful when.
You want to ignore any value not specified so we will be covering this pattern in more detail in our upcoming reviews now we will be seeing.
How we use patterns in our conditional F let so we have discussed how to use F let expression mainly.
As a shorter way to write the equivalent of a match that only matches one case optionally if let can have a.
Corresponding else containing code to run if the pattern in the F let doesn’t match so now we will be creating a program where we will.
Be matching all the possible values with a flag elsif and as a flat expression doing so gives us more flexibility than a match expression in which.
We can only express one value to compare with the patterns also the condition in a series of a flat else if and else if flat arms aren’t required to relate to each other the code that we will be creating will be showing a series of checks for several different condition that decide what the background color should.
Be so we will be creating a variable with hard-coded values that a real program might receive from user input if user specifies a favorite color that color is the background color if today is Tuesday the background color.
Will be green if the user specified their age a string and we can parse it as number successfully the color is either purple or orange depending on the value of the number if none of these conditions apply the background color will be blue so now let’s write the program on this.
Condition so what I am doing simply I am removing this now in my main function simply what I will be doing I am creating a variable let favorite color and Here I am specifying option so initially I’m storing none in it then I’m creating another variable that is it’s Tuesday and I am storing false in it then I’m creating another.
Variable named as H and it will be a result type and the okay will be off you a type.