Skip to content

Checkpoint: Count Only Completed Work

The Course
•
28 min study

A study log mixes sessions you have finished with sessions you only planned. A report that adds every duration together would claim work you have not done yet. In this checkpoint you combine the day's three tools: a vector holds the log, a struct keeps each session's minutes and status together, and a match on an enum decides which total each session belongs to.

What you will use

  • A vector that grows. push adds a session to a let mut vector, and len() counts everything logged.
  • A struct with an enum field. Each Session has minutes: u32 and status: Status, so the two facts cannot get out of step.
  • A borrowed loop. for session in &sessions reads each session and leaves the vector available afterward.
  • An exhaustive match. One arm per variant, with no wildcard, so a future variant forces you to decide where it counts.

Plan before you type

  1. Make the sessions binding mutable and push a completed 15-minute session.
  2. Create two counters, done and planned, both starting at 0.
  3. Loop over &sessions and match &session.status, adding session.minutes to the right counter.
  4. After the loop, print the completed total with the number of logged sessions, then the planned total.
  5. Change the planned session's duration and run again.
Prove the match decides

After your solution works, change the planned session from 30 to 300 minutes. Only the second line should change. If the first line changes too, the loop is adding every session instead of only the Done ones. Then swap the two arms' bodies on purpose and confirm that both numbers move to the wrong line.

Mistakes worth checking for

  • cannot borrow sessions as mutable: the binding was left as let sessions, so push is not allowed.
  • borrow of moved value: sessions at len(): the loop says for session in sessions and took ownership of the vector.
  • non-exhaustive patterns: &Status::Planned not covered: the match has only a Done arm. Add a Planned arm, even if it only counts the minutes.
  • Both totals look right, but the match ends with _ => {}: it works today, yet a new variant would quietly vanish from both totals. Name every variant instead.
  • The total says 70: the loop adds session.minutes before or outside the match, so planned work is counted.
Exercise

Your turn

The starter has one completed session and one planned session. Add a completed 15-minute session. Use a borrowed loop and a match to add each session's minutes to a completed or a planned total. Print Done: 40 minutes across 3 logged sessions and then Still planned: 30 minutes. Then change the planned duration and check that the completed total stays at 40.

Your working space Rust
enum Status {
    Planned,
    Done,
}

struct Session {
    minutes: u32,
    status: Status,
}

fn main() {
    let sessions = vec![
        Session { minutes: 25, status: Status::Done },
        Session { minutes: 30, status: Status::Planned },
    ];
    println!("Logged: {}", sessions.len());
}
Hint 1: the idea

The vector tracks every session. Each total only includes sessions in its own variant.

Hint 2: a first step

Inside a borrowed loop, match &session.status. Each arm is one line: Status::Done => done += session.minutes,.

Worked solution
Run the solution Rust
enum Status {
    Planned,
    Done,
}

struct Session {
    minutes: u32,
    status: Status,
}

fn main() {
    let mut sessions = vec![
        Session { minutes: 25, status: Status::Done },
        Session { minutes: 30, status: Status::Planned },
    ];
    sessions.push(Session { minutes: 15, status: Status::Done });

    let mut done = 0;
    let mut planned = 0;
    for session in &sessions {
        match &session.status {
            Status::Done => done += session.minutes,
            Status::Planned => planned += session.minutes,
        }
    }
    println!("Done: {done} minutes across {} logged sessions", sessions.len());
    println!("Still planned: {planned} minutes");
}
Text
Done: 40 minutes across 3 logged sessions
Still planned: 30 minutes

The shared borrow lets the loop read each session and lets main read the vector's length after the loop. Every session lands in exactly one total, so the two totals always add up to the sum of all durations, 70 here. That is a quick sanity check you can apply to any split like this.

Was this article helpful?

Your response is saved on this device.