Enums and Pattern Matching Flashcards
Enums allow you to ___
define a type by enumerating its possible values
Enums are similar to ___ in functional languages
algebraic data types
Enum values can only be
one of the variants
Enums are useful when you need to enumerate through
different possibilities of the same thing. For example IP4 vs IP6 addresses.
The different “kinds” of an enum are known as: ___
variants of the enum
varients of an enum are ____ under its ____.
namespaced; identifier
How do you put data into an enum?
The data can fit into each enum’s variant
What is the advantage of using an enum rather than a struct?
Each variant can have different types and amount of associated data
Can you put any kind of data inside an enum variant?
Yes, even other enums
What is an anonymous struct?
A struct without a name, usually defined inside an enum
Can you define methods on an enum?
Yes
What does “self” represent when defining a method on an enum?
The data passed in for the variant
What is the advantage of the Option enum in regards to the type system?
it means the compiler can check whether you’ve handled all the cases you should be handling
In languages with null, variables can always be in one of two states: _____ and _____.
null; not null
Does Rust support null?
No
What is the problem with null variables?
if you try to use a null value as a not-null value, you’ll get an error of some kind.
What does the Option enum express?
It encodes the concept of a value being present or absent
Why is having Option better than having null?
because Option and T are different types; the compiler won’t let us use an Option value as if it were definitely a valid value.