Skip to content

Checkpoint: Minutes That Count

The Course
•
25 min study

The planner only gives credit for real study. A session of 15 minutes or more counts in full; anything shorter counts as zero. In this checkpoint you split that job in two: a function that applies the rule to one session, and a loop in main that applies it to every session and keeps totals. Keeping the rule in one named function means that when the minimum changes, you edit exactly one line.

What you will use

  • A function with a parameter and a return type. credit(minutes: i32) -> i32 takes one duration and returns the minutes it earns.
  • An if expression as the tail. Both branches produce an i32, and the if is the function's value.
  • A for loop over an array. It visits each duration in order.
  • Two accumulators. One adds up the credit, the other counts the sessions that earned any.

Plan before you type

  1. Above main, write credit and decide whether 15 itself counts. The rule says "15 minutes or more", so it does.
  2. In main, create both accumulators before the loop.
  3. In the loop, call credit once per duration and store the result in a binding.
  4. Add the result to the total, and add one to the counter when the result is greater than zero.
  5. Print both lines after the loop.
Prove the boundary is tested

The data contains exactly 15, so it checks the boundary for you. Change >= to > in credit and run: the output should drop to Counted: 1 of 4 sessions and Credit: 25. If nothing changes, your function is not the one deciding. Then change the array to [5, 14] and expect Counted: 0 of 2 sessions and Credit: 0.

Mistakes worth checking for

  • Credit: 0 for every input: credit is called, but its result is never added to the total.
  • cannot find value total in this scope: the accumulator was declared inside the loop, so it does not exist where you print it.
  • Credit: 55: the loop adds minutes instead of credit(minutes), so short sessions count too.
  • expected i32, found (): a semicolon after the last expression in credit stopped it from returning a value.
  • Counted: 4 of 4 sessions: the counter increases on every iteration instead of only when the credit is greater than zero.
Exercise

Your turn

Write credit(minutes: i32) -> i32. Sessions of 15 minutes or more earn their full duration, and shorter sessions earn zero. Loop over [10, 15, 25, 5], call the function on each item and print Counted: 2 of 4 sessions and Credit: 40. Then try an array where every session is short.

Your working space Rust
fn main() {
    let durations = [10, 15, 25, 5];
    // Accumulate credit for each duration.
    println!("{durations:?}");
}
Hint 1: the idea

Give the rule for one item to the function. Give the work of visiting all items, and keeping the totals, to the loop.

Hint 2: a first step

Declare fn credit(minutes: i32) -> i32 above main. In the loop, write let earned = credit(minutes); and work from earned.

Worked solution
Run the solution Rust
fn credit(minutes: i32) -> i32 {
    if minutes >= 15 {
        minutes
    } else {
        0
    }
}

fn main() {
    let durations = [10, 15, 25, 5];
    let mut total = 0;
    let mut counted = 0;
    for minutes in durations {
        let earned = credit(minutes);
        total += earned;
        if earned > 0 {
            counted += 1;
        }
    }
    println!("Counted: {counted} of {} sessions", durations.len());
    println!("Credit: {total}");
}
Text
Counted: 2 of 4 sessions
Credit: 40

The two sessions that qualify earn 15 and 25 minutes. main never repeats the minimum of 15; it only asks credit, so changing the rule touches one line. An array of short sessions gives Counted: 0 and Credit: 0. Tomorrow you work with text, which Rust treats differently from numbers when you pass it around.

Was this article helpful?

Your response is saved on this device.