• expect(msg: &str) T: Returns value if Some, panics with custom message if None

Panics. Returning T instead of Option<T>

  • unwrap() T: Returns value if Some, panics if None
  • unwrap_or(T) T: Returns value if Some, otherwise default value
  • unwrap_or_else(() T) T: Like unwrap_or, but with closure
  • unwrap_or_default() T: Returns value if Some, otherwise default value for type T (requires Default trait)

Modify Option<T>. Closure returns U

  • map(T U) Option<U>: Transforms Some(T) to Some(U) with closure, None stays None
  • map_or(U, T U) U: Applies closure to Some(T) to get U, or returns default U if None
  • map_or_else(() U, T U) U: Applies closure G to Some(T) to get U, or closure F to get U if None

Modify Option<T>. Closure returns Option

  • and_then(T Option<U>) Option<U>: Chains operation, applying closure to Some(T) to produce another Option
  • filter(&T bool) Option<T>: Returns Some(T) if closure returns true, otherwise None
  • flatten() Option<U>: Flattens Option<Option<U>> into Option<U>
  • or_else(() Option<T>) Option<T>: Returns Some if present, otherwise applies closure

Convert Option<T> to Result<T, E> using:

  • ok_or(E) Result<T, E>: Returns Ok(T) if Some, otherwise Err(E) with provided error.
  • ok_or_else(() E) Result<T, E>: Like ok_or, but error is computed by closure if None.

Combine

  • and(Option<U>) Option<U>: Returns input Option if both are Some, otherwise None
  • or(Option<T>) Option<T>: Returns first Some, otherwise last None

Check

  • is_some() bool: Returns true if Some, otherwise false
  • is_none() bool: Returns true if None, otherwise false
  • contains(U) bool: Returns true if Some and value equals U, otherwise false