FlashKeepers

Rust · College · College and University

Rust III: Enums, Pattern Matching and Errors

Option, Result, match, if let, the ? operator, custom error types and panic versus recoverable errors.

About this set

40 basic cards for College Rust. Set 3 of 6 in Rust. It opens with "What does the Option enum represent in Rust?" and "What are the two variants of the Option enum?". Study it alongside the other College sets below.

40 cards · basic cards · AI-written, checked twice. Edit anything.

Study this set free Look inside first Get FlashKeepers for iPhone

Opens in your browser, no account. Get the iPhone and iPad app · Android app

What does the Option enum represent in Rust?
Show answerA value that may or may not be present, with variants Some(T) for a value and None for absence.
What are the two variants of the Option enum?
Show answerSome(T) for a present value, and None for absence.
What does Option::unwrap() do?
Show answerReturns the inner value T if it is Some(T), or panics if it is None.
What does Option::unwrap_or(default) do?
Show answerReturns the inner value if Some(T), or the provided default value if None.
What does Option::is_some() return?
Show answertrue if the Option is Some(T), false if it is None.
What does Option::is_none() return?
Show answertrue if the Option is None, false if it is Some(T).
What does Option::map(f) do?
Show answerApplies function f to the inner value if Some(T), returning Some(result), or returns None unchanged.
What does Option::filter(predicate) do?
Show answerReturns Some(T) if the predicate is true, or None if the predicate is false or the Option is already None.
What does Option::flatten() do?
Show answerConverts Option<Option<T>> to Option<T>, collapsing nested Options.
How do you use if let to destructure Some in an Option?
Show answerif let Some(x) = option { /* use x */ } matches and binds the inner value.
What does the Result enum represent in Rust?
Show answerA value that is either a success (Ok(T)) or an error (Err(E)), used for recoverable errors.
What are the two variants of the Result enum?
Show answerOk(T) for a successful value, and Err(E) for an error.
What does Result::unwrap() do?
Show answerReturns the inner Ok(T) value, or panics if the Result is Err(E).
What does Result::unwrap_or(default) do?
Show answerReturns the Ok(T) value, or the provided default value if Err(E).
What does Result::unwrap_err() do?
Show answerReturns the inner Err(E) value, or panics if the Result is Ok(T).

25 more cards in the app

Study this set free