Skip to content

Checkpoint: A Study Receipt

The Course
•
25 min study

This checkpoint puts the whole day into one short program: a receipt for today's study. It names its inputs, updates one of them, calculates a total and prints it two ways. The interesting part is not the arithmetic but the structure. If each input has its own name and every result is calculated from those names, changing one input updates the whole receipt, and you can check it by predicting the new output before you run.

What you will use

  • Immutable bindings for fixed inputs. The topic and the session length do not change while the program runs, so plain let is enough.
  • let mut and += for the count. The number of sessions starts at 2 and grows by one.
  • Integer arithmetic. * gives the total minutes; / and % split that total into hours and minutes.
  • Format strings. Named placeholders such as {topic} for bindings, and {} with a value after the text for a calculation.

Plan before you type

  1. Bind the topic to "Rust" and the session length to 25.
  2. Bind a mutable session count to 2, then add one.
  3. Calculate the total minutes from the count and the length, after the update.
  4. Split the total into hours and leftover minutes.
  5. Print the three lines, then change one input and predict the new receipt.
Prove the receipt follows its inputs

Change the session length to 20 and predict all three lines before you run. You should get 3 sessions, 60 minutes and 1 h 0 min. If the total still says 75, a number was typed directly into a calculation instead of using the binding. Then delete the += 1 line: the receipt should drop to 2 sessions. If the total does not drop with it, it was calculated before the update.

Mistakes worth checking for

  • cannot assign twice to immutable variable: the count was declared with let instead of let mut.
  • The total is 50 instead of 75: it was calculated before the session was added, so it used the old count.
  • invalid format string: a calculation was written inside the braces, such as {sessions * duration}. Bind it to a name first or use {}.
  • The hours line shows 1 h 75 min: the minutes were printed from the total instead of from the remainder % 60.
Exercise

Your turn

Store a topic, a session length of 25 minutes and an initial count of 2 sessions. Add one session. Print Rust: 3 sessions, Total: 75 minutes and That is 1 h 15 min on separate lines. Then change the session length to 20 and check your prediction.

Your working space Rust
fn main() {
    // Add your receipt here.
}
Hint 1: the idea

Keep the count and the length in separate bindings so you can change either input. Calculate everything else from them.

Hint 2: a first step

Start by binding the topic to "Rust", the duration to 25 and let mut sessions = 2;.

Worked solution
Run the solution Rust
fn main() {
    let topic = "Rust";
    let duration = 25;
    let mut sessions = 2;
    sessions += 1;
    let total = sessions * duration;
    let hours = total / 60;
    let minutes = total % 60;
    println!("{topic}: {sessions} sessions");
    println!("Total: {total} minutes");
    println!("That is {hours} h {minutes} min");
}
Text
Rust: 3 sessions
Total: 75 minutes
That is 1 h 15 min

The total uses the updated count because it is calculated after sessions += 1. With 20-minute sessions it becomes 60, which shows the boundary case 1 h 0 min: a remainder of zero is still printed. Tomorrow the planner starts making decisions, such as whether a session was long enough to count.

Was this article helpful?

Your response is saved on this device.