Ownership & Borrowing · 14 min · free lesson

Borrowing

Borrowing lets code use a value without taking ownership.

Core idea

Many shared borrows or one mutable borrow, never both at once.

Coming from C? Pointers to References covers this side by side. · Coming from Node.js? Mutating arguments vs borrowing covers this side by side.

Diagram for Borrowing: a Rust code example flows to the lesson rule and the key observation.
The borrow ends at its last use, and the push is fine after that.

Understand it

Rust permits many shared references or exactly one mutable reference, never both. The compiler tracks the region where each borrow is actually used, so a borrow ends at its last use rather than at the end of the block — which is why moving one line is often the whole fix.

Borrowing lets a function use data while its owner remains responsible for it. A shared reference, written &T, permits reading; many readers can coexist. A mutable reference, &mut T, permits writing but must have exclusive access for the duration of that borrow. This rule prevents a reader from holding a pointer into a vector while a writer grows the vector and possibly moves its buffer.

In the example, first points at the first element of items. The print is its last use. Only after that point does items.push(...) change the vector. Reverse those two lines and the shared borrow overlaps the mutation; the compiler rejects the code. The lexical braces are not the only clue: modern borrow checking follows the actual last use. This is why rearranging an operation can solve the conflict without adding a clone or another container.

For practice, first read two elements through separate shared references; both are allowed. Then try to push while either reference is still used later. Read the error as a timeline: identify when the read borrow starts, its final use, and where the write was attempted. Move the write after those uses or finish the reads first. Avoid treating every borrow error as a reason to copy data. Most are signals that the program needs a clearer order of operations. When the order truly cannot change, reconsider the structure that owns the data.

A shared borrow does not promise that the underlying allocation is immortal. It only prevents incompatible mutation while the reference is used. That distinction matters whenever a container might resize or be dropped.

Read the code

let mut items = vec![
    String::from("ada"),
];
let first = &items[0]; // borrow
println!("{first}");   // ...ends
items.push(String::from("grace"));

The borrow ends at its last use, and the push is fine after that.

Practice starter

let mut items = vec!["crab", "rust"];
let first = &items[0];
items.push("compiler");
println!("{first}");

Practise this code in Playground ↗

Try it

Repair E0502 with the smallest edit that keeps the push and the printed value.

What success looks like: Check passes, Run prints crab, and the Vec is still extended.

Check your understanding

What is the smallest edit that fixes it?

Reveal the answer
  1. Make items immutable
  2. Move the println! above the push
  3. Clone the whole vector

Ending the shared borrow before the mutation is the minimal change.

Go deeper

Common misconception: “An E0502 error means the data has to be cloned.”

The shared borrow only has to end before the mutation. Moving println!("{first}"); above items.push("compiler"); ends it at its last use, so no clone is needed.

  1. Mark the borrow: first borrows items at let first = &items[0];, and that shared borrow stays active until println!("{first}");, its last use.
  2. Predict before checking: items.push("compiler"); needs &mut items. Predict which line the compiler rejects, and which error code it reports.
  3. Compare with evidence: Check it: E0502 lands on the push. Move the println! above the push and check again: the diagnostic is gone, Run prints crab, and the Vec holds three items.

Transfer challenge: Read items[1] through a second shared reference and print both references before the push. Then move one of those prints after the push and find the borrow that E0502 names.

Connections

  • Ownership: Moves and scope
  • Slices: Borrowed views into data
Learn this lesson with AI

Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.

I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools.
Lesson: Borrowing (courseId "ownership", lessonId "borrowing")
Link: https://crabrix.com/learn/ownership/borrowing/
Goal: Many shared borrows or one mutable borrow, never both at once.
Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=ownership/borrowing) and I will write the code there.
Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.

In Claude Code, Codex or Cursor, paste it into a chat with Crabrix Academy connected. On a phone, the Claude and ChatGPT apps open the same links.