Skip to content

Checkpoint: Edit and Show a Title

The Course
•
28 min study

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)") returns true when 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

  1. Write show with a &str parameter that prints the title.
  2. Write finish with a &mut String parameter.
  3. Inside finish, append the suffix only when the title does not already end with it. ! in front of a boolean turns true into false and back.
  4. 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 in main needs let mut.
  • mismatched types at a call to finish: the call passes &topic instead of &mut topic.
  • borrow of moved value: topic: a function takes String instead of a reference, so the first call moved the title away.
  • The suffix appears without its space: the text passed to push_str must be " (complete)", starting with a space.
Exercise

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.

Your working space Rust
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
Run the solution Rust
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);
}
Text
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.

Was this article helpful?

Your response is saved on this device.