foreign
here for infoworld in this episode of smart rust I'm going to introduce the concept of the match
keyword and how it can be used to make decisions based on either the value of something or its type
matching based on values or types unlocks a great many possibilities in Rust programs
the idea of matching on a value making a decision based on the contents of a variable
ought to be familiar but matching on a type is in many ways more powerful
in earlier videos I introduced the concept of the option type which can either be a
value of some given type or Nothing at All we can use match to work with option types
to make decisions that might otherwise not be possible to encode safely in values alone
so let's start with a simple example of match just using values so you can get an idea of the syntax
in this program I simulate rolling a die 10 times and I output some string based on the results the
die rolls are integers from one through six and we evaluate the results in the die roll function
and matches syntax as you can see is simple enough you give it what item you want to match with you
list possible values for a match and then you use the arrow syntax to provide an expression
to return as a result of the match in this case we're matching against three possible values one
four and six and the underscore at the end for a match value is a wild card match that's what
we use if nothing else matches note that there was no fall through for matches any one match
will terminate the matching operation now if we run this code you'll see it runs 10 die rolls
and it gives us the appropriate string for each one including our wildcard case something else
but let's say we want to do something a little more advanced we want to handle
situations where sometimes we have a number and sometimes we don't have anything at all
also we want to use rust's type system to discriminate between those things at compile time
see we could if we wanted to we could use an integer value of 0 or
-1 or something to represent some special condition but we want to reserve integer
values any integer values for actual die rolls we want something that's not an integer to be an
entirely different type we want something that's not a die roll to be an entirely different type
so let's look at a different version of this code for our die roll we don't use an integer anymore
we use an option I8 type this is a type that could be an integer but could also be nothing
so each time we roll the dice we now have a random factor that gives us a one in three chance of
coming up with none or two and three chance of generating an integer die roll so now when we
pass on our option I8 value to die roll we use two match functions the first is to match against the
type we test what type that passed in value is if it's none we say the die fell off the table
if it's an actual integer value then we take that integer value s we match against it
and we return one of our earlier responses including our wildcard
so if we run this code we now get a few instances of the die falling off the table
with a value of none and again we could use 0 or -1 to indicate fell off the table but
this way we don't have to do that we can have our die rolls expressed as integer values and
everything else roles that fall off the table for instance expresses a completely different
type and we can use match to choose which path to take based on the types in question
so to sum up the match keyword in Rust lets us do more than match against values it can
also match against types and when used with the option type it lets us make
unambiguous decisions about what we want to do since types are determined at compile time
so that's it for this video if you liked it please leave a comment below don't forget
to subscribe to the tech talk Channel on YouTube and for more smart rust and more
Dev with sirdar be sure to follow me on Facebook YouTube and infoworld.com