This checkpoint keeps one String in main for the whole program and lends it out twice over: once to a function that only shows it, and once to a function that changes it. Your job is to choose the right kind of borrow for each, so that main stays the owner from start to finish. To make it a little more realistic, finishing a topic twice must not mark it twice, the way a real planner would ignore a second click on "complete".
What you will use
- A shared borrow for reading.
show(topic: &str)prints the title and cannot change it. - A mutable borrow for changing.
finish(topic: &mut String)appends(complete)in place. - A check before changing.
topic.ends_with(" (complete)")returnstruewhen the text already ends with the suffix. - Borrows that do not overlap. Each call's borrow ends when the call returns, so the next call can borrow again.
Plan before you type
- Write
showwith a&strparameter that prints the title. - Write
finishwith a&mut Stringparameter. - Inside
finish, append the suffix only when the title does not already end with it.!in front of a boolean turnstrueintofalseand back. - In
main, show the title, finish it, show it, finish it again and show it once more.
Prove the check does its job
Remove the if from finish so it always appends, and run. The last line should become Ownership (complete) (complete). If it does not, the second call to finish is missing. Put the check back and both finished lines should be identical.
Mistakes worth checking for
cannot borrow topic as mutable, as it is not declared as mutable: the String inmainneedslet mut.mismatched typesat a call tofinish: the call passes&topicinstead of&mut topic.borrow of moved value: topic: a function takesStringinstead of a reference, so the first call moved the title away.- The suffix appears without its space: the text passed to
push_strmust be" (complete)", starting with a space.
Your turn
Write show to print a borrowed title and finish to append (complete) in place, but only if the title does not already end with it. In main, show the title Ownership, finish it, show it, finish it again and show it a last time. Use the same String throughout.
fn main() {
let mut topic = String::from("Ownership");
// Show, finish, show, finish again, show.
println!("{topic}");
}Hint 1: the idea
Choose the access each function needs. Showing only reads, while finishing changes the original. The check reads the text before deciding whether to change it.
Hint 2: a first step
Give show a parameter of type &str. In finish, start with if !topic.ends_with(" (complete)") { ... }.
Worked solution
fn show(topic: &str) {
println!("{topic}");
}
fn finish(topic: &mut String) {
if !topic.ends_with(" (complete)") {
topic.push_str(" (complete)");
}
}
fn main() {
let mut topic = String::from("Ownership");
show(&topic);
finish(&mut topic);
show(&topic);
finish(&mut topic);
show(&topic);
}Ownership
Ownership (complete)
Ownership (complete)Each call's borrow ends before the next call begins, and main owns the String throughout. The second finish found the suffix already there and left the text alone. finish can read through its &mut reference too: a mutable borrow allows reading as well as writing. Tomorrow you keep many sessions in a list that can grow, and borrowing decides how a loop reads it.
Completion is saved in this browser only. It does not measure mastery.