Skip to main content

Make the borrow checker your friend

The rule is short: any number of shared `&` borrows, or exactly one `&mut` borrow, never both at the same time. That single constraint is what removes data races at compile time.

Each step below is a small program. Type it, watch the live result, and let the checkpoints confirm you got it.

Step 1 of 3

Read without taking ownership

Write a function `fn length(text: &String) -> usize` (or `&str`) and call it, then print the original string afterwards.

A shared borrow gives read access. The owner stays valid, so the value is still yours after the call.

Checkpoints

  • A function takes a reference parameter — not yet passed
  • The program compiles and runs without errors — not yet passed
  • The original string is still printed after the call — not yet passed