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.
pushadds a session to alet mutvector, andlen()counts everything logged. - A struct with an enum field. Each
Sessionhasminutes: u32andstatus: Status, so the two facts cannot get out of step. - A borrowed loop.
for session in &sessionsreads 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
- Make the
sessionsbinding mutable and push a completed 15-minute session. - Create two counters,
doneandplanned, both starting at 0. - Loop over
&sessionsandmatch &session.status, addingsession.minutesto the right counter. - After the loop, print the completed total with the number of logged sessions, then the planned total.
- 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 aslet sessions, sopushis not allowed.borrow of moved value: sessionsatlen(): the loop saysfor session in sessionsand took ownership of the vector.non-exhaustive patterns: &Status::Planned not covered: the match has only aDonearm. Add aPlannedarm, 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.minutesbefore or outside the match, so planned work is counted.
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.
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
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");
}Done: 40 minutes across 3 logged sessions
Still planned: 30 minutesThe 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.
Completion is saved in this browser only. It does not measure mastery.