Set the course aside for a few days. Then try these with the hints closed. Pulling an idea back out of memory, even slowly and with mistakes, makes it stick far better than rereading the lesson. If you get stuck, that is useful information: it shows you which lesson to revisit.
For each challenge, write your answer, run it, then make one small change and explain the new output before running it. The three challenges cover the week's main threads: borrowing instead of moving, separating a failed conversion from an unwanted value, and letting a method guard the data it owns.
Reuse an owned title
Your turn
Write a function that reads a title through a borrow and prints Next: Rust. Call it twice with the same owned String and print Saved: Rust afterward.
fn main() {
let title = String::from("Rust");
println!("Saved: {title}");
}Hint 1: the idea
The function can read the text through a shared reference. Main keeps ownership.
Hint 2: a first step
Use a parameter of type &str and pass &title at each call.
Worked solution
fn show(topic: &str) {
println!("Next: {topic}");
}
fn main() {
let title = String::from("Rust");
show(&title);
show(&title);
println!("Saved: {title}");
}Next: Rust
Next: Rust
Saved: RustBoth calls borrow the same text, one call at a time, and main keeps ownership throughout. If you wrote fn show(topic: String) and show(title), the second call fails with E0382 because the first call moved the string. Passing &title where a &str is expected works because Rust converts a &String to a &str for you.
Handle a failed conversion
Your turn
Parse ["15", "later", "0"] as unsigned integers. Print Keep 15, Invalid number, and Skip zero on separate lines. Then add one more valid value and predict the extra output.
fn main() {
for text in ["15", "later", "0"] {
println!("{text}");
}
}Hint 1: the idea
A successful parse and a useful value are two different decisions. Zero parses successfully.
Hint 2: a first step
Use match text.parse::<u32>() and give zero a dedicated Ok(0) arm.
Worked solution
fn main() {
for text in ["15", "later", "0"] {
match text.parse::<u32>() {
Ok(0) => println!("Skip zero"),
Ok(minutes) => println!("Keep {minutes}"),
Err(_) => println!("Invalid number"),
}
}
}Keep 15
Invalid number
Skip zeroThe specific Ok(0) pattern comes before the general Ok(minutes) pattern; in the other order, zero would print Keep 0 and the compiler would warn about an unreachable pattern. A guard such as Ok(minutes) if minutes > 0 would work too. Adding "30" prints one more line, Keep 30, in the position where you added it.
Guard a change with a method
Your turn
Give Log a method add(&mut self, minutes: u32) -> Result<(), &'static str> that stores durations from 1 to 240 and otherwise returns Err("use 1–240 minutes") without storing anything. Add a total(&self) -> u32 method. In main, add 30, 0, 300 and 45, print Rejected 0: use 1–240 minutes for each rejection, then print Total: 75 minutes across 2 sessions.
struct Log {
sessions: Vec<u32>,
}
fn main() {
let log = Log { sessions: Vec::new() };
println!("Sessions: {}", log.sessions.len());
}Hint 1: the idea
Validate first, and push only on the success path, as the planner's add_line does.
Hint 2: a first step
Write impl Log with fn add(&mut self, minutes: u32). Loop over [30, 0, 300, 45] in main and match on each call's result.
Worked solution
struct Log {
sessions: Vec<u32>,
}
impl Log {
fn add(&mut self, minutes: u32) -> Result<(), &'static str> {
if minutes < 1 || minutes > 240 {
return Err("use 1–240 minutes");
}
self.sessions.push(minutes);
Ok(())
}
fn total(&self) -> u32 {
let mut total = 0;
for minutes in &self.sessions {
total += *minutes;
}
total
}
}
fn main() {
let mut log = Log { sessions: Vec::new() };
for minutes in [30, 0, 300, 45] {
match log.add(minutes) {
Ok(()) => {}
Err(message) => println!("Rejected {minutes}: {message}"),
}
}
println!("Total: {} minutes across {} sessions", log.total(), log.sessions.len());
}Rejected 0: use 1–240 minutes
Rejected 300: use 1–240 minutes
Total: 75 minutes across 2 sessions|| means "or", so the condition is true when either end of the range is broken. The early return keeps the push on the success path only, which is why the total stays at 75. If you can write this from memory, you have the core of the study planner: a struct, a guarded &mut self method returning Result, and a read-only &self method. The intermediate course builds on exactly these pieces.
Completion is saved in this browser only. It does not measure mastery.