Enums

An enum may contain many types including other enums.

pub enum Error {
    StateError: StateError,
    UserError: UserError,
}

pub enum StateError {
    Void: (),
    Pending: (),
    Completed: (),
}

pub enum UserError {
    InsufficientPermissions: (),
    Unauthorized: (),
}

Encouraged

The preferred way to use enums is to use the individual (not nested) enums directly because they are easy to follow and the lines are short:

    let error1 = StateError::Void;
    let error2 = UserError::Unauthorized;

Discouraged

If you wish to use the nested form of enums via the Error enum from the example above, then you can instantiate them into variables using the following syntax:

    let error1 = Error::StateError(StateError::Void);
    let error2 = Error::UserError(UserError::Unauthorized);

Key points to note:

  • You must import all of the enums i.e. Error, StateError & UserError
  • The lines may get unnecessarily long (depending on the names)
  • The syntax is unergonomic