Summary
-
An
enum
creates a new data type -
An
enum
data type can have different variants -
Each
enum
value is always one of those variants -
The variants can have 0 or more fields
-
Using the
match
expression, you can pattern match against values to determine which variant you’re using -
Match arms have a pattern, a fat arrow
=>
, and an expression -
If your arm’s expression is a block, the comma following the block is optional
-
enum
s can haveimpl
blocks, trait implementations, and can derive traits -
enum
s can have type parameters -
A common pattern in Rust is to use the
Option<T>
enum to represent an optional result -
Using
match
expressions, you can capture the values in fields into a variable
Get hands-on with 1400+ tech skills courses.