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) -> i32takes one duration and returns the minutes it earns. - An
ifexpression as the tail. Both branches produce ani32, and theifis the function's value. - A
forloop 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
- Above
main, writecreditand decide whether 15 itself counts. The rule says "15 minutes or more", so it does. - In
main, create both accumulators before the loop. - In the loop, call
creditonce per duration and store the result in a binding. - Add the result to the total, and add one to the counter when the result is greater than zero.
- 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: 0for every input:creditis 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 addsminutesinstead ofcredit(minutes), so short sessions count too.expected i32, found (): a semicolon after the last expression increditstopped 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.
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.
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
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}");
}Counted: 2 of 4 sessions
Credit: 40The 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.
Completion is saved in this browser only. It does not measure mastery.