Enums and Pattern Matching Flashcards

1
Q

What is enum short for?

A

Enumerations.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What do enums allow for?

A

Define a type by enumerating its possible values.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the keyword used to create an enum?

A

enum.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What do we call the possible values of an enum?

A

The variants.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How are variants of an enum namespaced?

A

Under their identifier. syntax: idntifier::variant;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do we attach data to an enum variant?

A

Comma separated list of each data type inside parentheses or an anonymous struct.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do we define methods for enums

A
impl enum_name {
    fn foo(&self)...
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the Option enum?

A

Enum defined by the standard library that encodes a common scenario in which a value could be something or it could be nothing.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does Rust have in place of nulls?

A

The enum Option

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How is Option defined in the standard library?

A

enum Option {
Some(T),
None,
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does it mean when we have a Some value?

A

That a value is present and the value is held within the Some.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does it mean when we have a None value?

A

The is no valid value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Why is Option better than null?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What does the match control flow operator allow for?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What does it mean that Matches are exhaustive in Rust?

A

All possibilities of the match must be handled for the code to compile.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What will _ match in match operation?

A

All cases that are not specified before it.

17
Q

What does the if let control flow allow for?

A

A less verbose way to handle values that match one pattern while ignoring the rest.

18
Q

What does an if let statement do?

A

if let permits patterns matching within the condition of an if statement. … If a pattern matches successfully, it binds any appropriate parts of the value to the identifiers in the pattern, then evaluates the expression. If the pattern doesn’t match, nothing happens.

19
Q

What is an example of an if let statement?

A

if let Some(3) = some_u8_value {
println!(“three”);
}