CH 6. Enums and Pattern Matching Flashcards

1
Q

What is an enum in Rust?

A

An enum, short for enumeration, is a custom data type that allows you to 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

How do you define an enum in Rust?

A

You define an enum using the enum keyword followed by the name of the enum and a list of its variants. For example:

enum Coins {
penny,
nickel,
dime,
quarter
}

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

What are the two types of enums in Rust?

A

Rust has two types of enums: those with named variants and those with unnamed variants (also known as unit-like or tuple-like). For example:

// named variants
enum Color {
Red,
Green,
Blue,
}

// Unnamed variants
enum Fruit {
Apple(String),
Banana(String),
Orange(String),
}

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

How do you use the match expression in Rust?

A

The match expression allows you to compare a value against a series of patterns and execute code based on the matched pattern.

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

What is exhaustive pattern matching?

A

Exhaustive pattern matching ensures that every possible case is handled in the match expression, leaving no unmatched patterns.

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

What is the purpose of the _ wildcard pattern in Rust?

A

The _ wildcard pattern is used as a catch-all for cases that are not explicitly defined in the match expression.

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

What is the if let expression in Rust?

A

The if let expression provides a more concise way to handle a single pattern match and execute code if the pattern matches.

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

What is the purpose of the Option enum in Rust?

A

The Option enum represents a value that can either be Some(value) or None, providing a way to handle optional values and potential absence of data.

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

What is the Result enum used for in Rust?

A

The Result enum is similar to Option but is typically used to handle operations that may produce an error. It can have two variants: Ok(value) or Err(error).

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

How can you bind values to variables within a pattern match?

A

By using the @ symbol followed by a pattern, you can bind the matched value to a variable for further use within the corresponding code block.

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