Reading a project is one skill; changing it without breaking it is the next. A good first change adds behavior at the edge of the program and leaves the rules alone. Here you add a study goal to the end of the report. It only reads from the planner, through the same public methods the report already uses, so it cannot affect which sessions are accepted, and the checks from the previous lesson stay valid.
The goal is 60 minutes. The interesting input is the one that hits it exactly, because that is where > and >= give different answers. The supplied input adds up to exactly 60, so it tests the boundary on the first run.
Add a study goal
At the end of main.rs, add a message for a 60-minute study goal. Print Goal reached at 60 minutes or more. Otherwise print Keep going. Run the supplied input for the exact boundary. Then change the first duration from 25 to 24 and verify the other branch.
Hint 1: the idea
Use the method that reports the total after all input lines have been handled.
Hint 2: a first step
Bind the result of planner.total_minutes() immediately before your comparison, after the line that prints Sessions: ....
Worked solution
Add this inside main, after the report.
let total = planner.total_minutes();
if total >= 60 {
println!("Goal reached");
} else {
println!("Keep going");
}With the supplied input, the last line is Goal reached. At 59 minutes it becomes Keep going. Writing total > 60 would print Keep going at exactly 60, a bug that only the boundary input reveals. The rejected Review,later line does not count toward the goal, because only stored sessions are in the total.
Try another extension
Name the longest session
After the goal message, print the longest session as Longest: Ownership (35 min) for the supplied input. When there are no sessions, print Longest: none (0 min). If two sessions tie, keep the first one.
Hint 1: the idea
Loop over planner.sessions() and remember the largest duration seen so far, together with its topic.
Hint 2: a first step
Start with let mut longest = 0; and let mut topic = "none";. Inside the loop, update both only when session.minutes > longest.
Worked solution
let mut longest = 0;
let mut topic = "none";
for session in planner.sessions() {
if session.minutes > longest {
longest = session.minutes;
topic = session.topic.as_str();
}
}
println!("Longest: {topic} ({longest} min)");as_str() borrows the stored String as a &str, so topic can hold either the literal "none" or a view of a stored topic. The borrow is fine because the planner lives until the end of main. Using > rather than >= is what keeps the first of two equal sessions. Clear the input to check the empty case.
Run with Cargo
Everything so far has run in the browser. To run the planner on your own computer, download the study planner ZIP and extract it. The ZIP contains the original project, without your changes. You need a local Rust toolchain that supports the 2024 edition, Rust 1.85 or later; installing it is optional for this course.
Cargo is Rust's build tool and package manager. It reads Cargo.toml, which names the package and lists its two programs, called binaries:
[package]
name = "study-planner"
version = "0.1.0"
edition = "2024"
default-run = "study-planner"
[[bin]]
name = "study-planner"
path = "main.rs"
[[bin]]
name = "check-planner"
path = "check.rs"Open a terminal in the extracted directory that contains Cargo.toml and run:
printf 'Loops,25\nOwnership,35\nReview,later\n' | cargo run --quiet
cargo run --quiet --bin check-plannerThe first command builds the default binary and runs it. printf writes the three lines, and the | pipe sends them to the planner's stdin, just like the browser's stdin box. The second command selects the check program by name. --quiet hides Cargo's own progress messages, so you see only the program's output. The first build takes a little longer; Cargo stores the compiled program in a target folder and reuses it until a file changes.
You can also run cargo run alone and type requests yourself. Press Enter after each line, then end the input with Ctrl+D on macOS and Linux, or Ctrl+Z followed by Enter on Windows. The browser runs the same Rust files with its own compiler, so the output should match line for line. To keep your extensions, copy them into the downloaded main.rs and run both commands again.
Going further: Rust's own test runner
check.rs is a second main that panics on the first failed assertion. That shape runs in the browser, which has no test runner. On your own computer, Rust has one built in, and nearly every Rust project you read next uses it instead of a check binary. The checks move into functions marked #[test], inside a module that is compiled only for testing. Add this to the end of the downloaded main.rs:
#[cfg(test)]
mod tests {
use crate::planner::Planner;
#[test]
fn trims_and_stores_a_valid_line() {
let mut planner = Planner::new();
assert_eq!(planner.add_line(" Loops , 1 "), Ok(()));
assert_eq!(planner.sessions()[0].topic, "Loops");
assert_eq!(planner.total_minutes(), 1);
}
#[test]
fn rejects_minutes_above_240_without_storing() {
let mut planner = Planner::new();
assert_eq!(planner.add_line("Review,241"), Err("use 1–240 minutes"));
assert_eq!(planner.sessions().len(), 0);
}
}Then run cargo test. Among Cargo's lines you will see:
running 2 tests
test tests::rejects_minutes_above_240_without_storing ... ok
test tests::trims_and_stores_a_valid_line ... ok#[cfg(test)] leaves the module out of the normal program, so cargo run is unchanged. crate::planner names the planner module from the top of the program, because tests is a module of its own. The assertions are the same assert_eq! you already know. What changes is that each #[test] function runs separately with its own fresh Planner, and a failing test is reported by name while the others still run, where check.rs stops at the first failure. Cargo also reports running 0 tests for the check-planner binary, which has no #[test] functions.
Completion is saved in this browser only. It does not measure mastery.