hi there sardari aguilar pierre for infoworld in this episode of smartrust i'm going to take
a quick tour of rust's enum type those of you who have come in from c and c
plus should recognize the concept of an enum it's even called the same thing in those languages
an enum is a way to take a variety of types and create values that could be any one of those types
and this gives you some powerful metaphors that you can use to structure your program's logic with
in fact even though it might not seem that way at first enums are a big part of how
rust's type system makes it possible to write safer and more robust software
in this example i've got a program that simulates automating some interaction with a web service
and to control the events i have an enum named appropriately enough event and an instance of
event can take one of three types the first type is named fetch url and that's simply a string
the second type is pause and that's a struct which consists of two things a u64 value
which is a delay time in milliseconds and a string message to display while waiting
the third value is quit and that takes no parameters
and again the idea here is that an instance of an event could be any of these three things
encoding it as an enum lets us choose between them and it also lets us later perform operations
based on what type of event it is and again this sounds trivial but it is actually at
the heart of some of rust's most powerful features and i'm going to step through why
first off when you construct an enum you use the double colon syntax to refer to the subtype that
you want to use this is the same kind of thing that we do when we want to refer to something
in a module so when we want to create a fetch url enum we use this syntax for a pause enum we
use this since we need to provide values for the names in the structure that's inside it
and for quit we just say event quit now to do something with these enums we're going to use
a function named run and we're going to pass each of these enums into run what we do inside
the function is again a demonstration of why enums can be as powerful as they are
in rust we have a keyword match and what match lets us do is take an enum
and perform one of a number of actions based on what type that enum is so in this case we have
three choices if the type of our edom is fetch url then we will print the url that we're fetching
again this is a simulation an actual program of this kind would do the url fetching for real
and if the type is pause then we print the message portion of the pause structure
and wait the number of milliseconds indicated by the ms value in the structure now notice that
we're passing the named values from the enum into this block and we're reusing them we could also
call a function for this if we needed to but this block is small enough that isn't really required
and finally if the type is quit we just print a closing message so if we run this code you'll see
that it performs all of these actions in turn and there's the appropriate delay for the wait time
and then we're finished we have the quit message now what makes an enum so useful
as we have seen here is that you can use the type information encoded into an enum
at compile time to make decisions at run time type information about objects is set down at
compile time and rust's type system is very strict about using right types for things consistently
so as a result the types that we're using for things will go a long way towards documenting what
they're for and how they're used that means things like the match operation are going to be highly
predictable at runtime there's less opportunity to be unpleasantly surprised about the way things
might behave at runtime and if you're trying to write robust software that's really important
now in future videos i'm going to dive a little further into the way match works as an adjunct
to enums and i'm also going to explore an enum that is built into rust called the option type
and this will allow us to unlock even more powerful ways to encode values into rust's type
system so for now to sum up enums let us create variables that are one of a set number of types
we can use what type an enum is to make decisions about what to do at runtime and because those
types are declared at compile time they make rust programs much more rigorous and predictable
so that's it for now if you like this video leave a comment below
don't forget to subscribe to the tech talk channel on youtube and for more smart rust
and more of dev with serdar be sure to follow me on facebook youtube and infoworld.com