Option, Result, match, if let, the ? operator, custom error types and panic versus recoverable errors.
40 cards · basic cards · AI-written, checked twice. Edit anything.
- What does the Option enum represent in Rust?
Show answer
A 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 answer
Some(T) for a present value, and None for absence.
- What does Option::unwrap() do?
Show answer
Returns the inner value T if it is Some(T), or panics if it is None.
- What does Option::unwrap_or(default) do?
Show answer
Returns the inner value if Some(T), or the provided default value if None.
- What does Option::is_some() return?
Show answer
true if the Option is Some(T), false if it is None.
- What does Option::is_none() return?
Show answer
true if the Option is None, false if it is Some(T).
- What does Option::map(f) do?
Show answer
Applies function f to the inner value if Some(T), returning Some(result), or returns None unchanged.
- What does Option::filter(predicate) do?
Show answer
Returns 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 answer
Converts Option<Option<T>> to Option<T>, collapsing nested Options.
- How do you use if let to destructure Some in an Option?
Show answer
if let Some(x) = option { /* use x */ } matches and binds the inner value.
- What does the Result enum represent in Rust?
Show answer
A 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 answer
Ok(T) for a successful value, and Err(E) for an error.
- What does Result::unwrap() do?
Show answer
Returns the inner Ok(T) value, or panics if the Result is Err(E).
- What does Result::unwrap_or(default) do?
Show answer
Returns the Ok(T) value, or the provided default value if Err(E).
- What does Result::unwrap_err() do?
Show answer
Returns the inner Err(E) value, or panics if the Result is Ok(T).