ProgramGuru

Enums and optional values

Enums and optional values

A missing mark is different from a mark of zero. Option expresses either Some(value) or None, making absence visible in the type. An enum defines a finite set of variants, each of which can carry its own data.

What you’ll learn

  • Represent absence separately from a recorded zero.
  • Handle data-carrying variants with exhaustive patterns.

Enums and optional values in action

  1. Marked(80): known mark

    The Marked variant carries an integer that the pattern binds to score.

    Marked(80): known mark
  2. NotSubmitted: no submission

    NotSubmitted carries no mark and produces a separate message.

    NotSubmitted: no submission
  3. Option None: no stored value

    None indicates that no integer was recorded; it does not silently become zero.

    Option None: no stored value
The Marked variant carries an integer that the pattern binds to score. NotSubmitted carries no mark and produces a separate message. None indicates that no integer was recorded; it does not silently become zero.

Enums and optional values explained

Match handles the variants explicitly. if let handles one selected pattern when the other cases need no action. Option does not automatically unwrap itself; the program must choose what absence means. A custom enum can distinguish states such as NotSubmitted and Marked(score) more clearly than unexplained numbers.

Work through the Rust program

Save the complete source as main.rs in the Rust environment. No standard input is needed.

Rust 2024 · complete program

enum Submission { NotSubmitted, Marked(u32) }
fn label(value: Submission) -> String {
  match value {
    Submission::NotSubmitted => "Awaiting work".to_string(),
    Submission::Marked(score) => format!("Mark: {score}"),
  }
}
fn main() {
  println!("{}", label(Submission::Marked(80)));
  println!("{}", label(Submission::NotSubmitted));
  let missing: Option<u32> = None;
  println!("Recorded: {}", missing.is_some());
}

Expected output

Mark: 80
Awaiting work
Recorded: false

Follow the execution

The Marked variant carries an integer that the pattern binds to score. NotSubmitted carries no mark and produces a separate message. None indicates that no integer was recorded; it does not silently become zero.

Try the complete program in the Rust compiler. For tests, Cargo commands and local files, use the specified local environment.

Try a small variation

Your task: Should an absent submission automatically receive the recorded mark zero?

Rust 2024 · complete program

enum Submission { NotSubmitted, Marked(u32) }
fn label(value: Submission) -> String {
  match value {
    Submission::NotSubmitted => "Awaiting work".to_string(),
    Submission::Marked(score) => format!("Mark: {score}"),
  }
}
fn main() {
  println!("{}", label(Submission::Marked(80)));
  println!("{}", label(Submission::NotSubmitted));
  let missing: Option<u32> = None;
  println!("Recorded: {}", missing.is_some());
}

Worked answer

Only if the school explicitly chooses that policy. Absence and a genuine zero are different states, so keep them distinct until the reporting rule decides what to do.

Mistakes and limits to recognize

Unwrap on None panics. A fallback is useful only when its meaning is valid for the domain. Exhaustive matching can make a newly added enum variant trigger a useful compiler reminder to update handling code.

Check your understanding

Should an absent submission automatically receive the recorded mark zero?
Choose one answer. You can try again.

Answer and explanation

Only if the school explicitly chooses that policy.

Only if the school explicitly chooses that policy. Absence and a genuine zero are different states, so keep them distinct until the reporting rule decides what to do.

Take the idea into your next program

Only if the school explicitly chooses that policy. Absence and a genuine zero are different states, so keep them distinct until the reporting rule decides what to do. Unwrap on None panics.

Further reading: the official Rust Book on enums and optional values.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Result and recoverable errors →