Enums and Pattern Matching Flashcards
What is enum short for?
Enumerations.
What do enums allow for?
Define a type by enumerating its possible values.
What is the keyword used to create an enum?
enum.
What do we call the possible values of an enum?
The variants.
How are variants of an enum namespaced?
Under their identifier. syntax: idntifier::variant;
How do we attach data to an enum variant?
Comma separated list of each data type inside parentheses or an anonymous struct.
How do we define methods for enums
impl enum_name { fn foo(&self)... }
What is the Option enum?
Enum defined by the standard library that encodes a common scenario in which a value could be something or it could be nothing.
What does Rust have in place of nulls?
The enum Option
How is Option defined in the standard library?
enum Option {
Some(T),
None,
}
What does it mean when we have a Some value?
That a value is present and the value is held within the Some.
What does it mean when we have a None value?
The is no valid value.
Why is Option better than null?
Because Option and T (where T can be any type) are different types. Any value of Type T is guaranteed to be not null. If a value is of Option, the null case MUST be explicitly handled.
What does the match control flow operator allow for?
Comparing a value against a series of patterns and then executing code based on which pattern matches. Also match arms can bind to the parts of the values that match the pattern.
What does it mean that Matches are exhaustive in Rust?
All possibilities of the match must be handled for the code to compile.