Enums and Pattern Matching Flashcards

1
Q

Enums allow you to ___

A

define a type by enumerating its possible values

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

Enums are similar to ___ in functional languages

A

algebraic data types

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

Enum values can only be

A

one of the variants

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

Enums are useful when you need to enumerate through

A

different possibilities of the same thing. For example IP4 vs IP6 addresses.

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

The different “kinds” of an enum are known as: ___

A

variants of the enum

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

varients of an enum are ____ under its ____.

A

namespaced; identifier

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

How do you put data into an enum?

A

The data can fit into each enum’s variant

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

What is the advantage of using an enum rather than a struct?

A

Each variant can have different types and amount of associated data

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

Can you put any kind of data inside an enum variant?

A

Yes, even other enums

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

What is an anonymous struct?

A

A struct without a name, usually defined inside an enum

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

Can you define methods on an enum?

A

Yes

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

What does “self” represent when defining a method on an enum?

A

The data passed in for the variant

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

What is the advantage of the Option enum in regards to the type system?

A

it means the compiler can check whether you’ve handled all the cases you should be handling

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

In languages with null, variables can always be in one of two states: _____ and _____.

A

null; not null

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

Does Rust support null?

A

No

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

What is the problem with null variables?

A

if you try to use a null value as a not-null value, you’ll get an error of some kind.

17
Q

What does the Option enum express?

A

It encodes the concept of a value being present or absent

18
Q

Why is having Option better than having null?

A

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.