Ownership in Practice · 14 min · free lesson

Mutable Borrows

One temporary writer with exclusive access.

Core idea

An &mut reference permits mutation without transferring ownership.

Coming from Node.js? Mutating arguments vs borrowing covers this side by side.

Diagram for Mutable Borrows: a Rust code example flows to the lesson rule and the key observation.
The example makes the contract visible in code.

Understand it

An &mut reference permits mutation without transferring ownership. Exclusivity lasts through its final use, preventing another reader or writer from observing a half-finished update.

An &mut T gives one caller exclusive temporary access to a value. The owner still exists, but it cannot read or write through another path while the mutable borrow is active. This is more than a rule about syntax: it prevents a reader from seeing a partially updated structure and prevents two writers from racing to change the same state. After the last use of the borrow, the owner can be used again.

In add_one, the star in *value += 1 follows the mutable reference to the integer it points at. score must be declared mut so it can be borrowed mutably. The call lends access; it does not move score into the function. The final print shows the updated owned value. Try storing let edit = &mut score;, then attempt to print score before edit is last used. The compiler flags the overlapping access. Move the print after that last use and the program becomes valid.

The same pattern applies to methods on vectors and structs. A mutable borrow lets a helper change an object in place without returning a replacement object. Keep the exclusive region short and meaningful. If a large function needs many unrelated mutable borrows, split it into operations with clear boundaries. This can make borrow errors easier to diagnose and can reveal which fields should be separate. Reach for interior mutability only when ordinary exclusive borrowing cannot express the actual access pattern.

Read the code

fn add_one(value: &mut i32) {
    *value += 1;
}
let mut score = 6;
add_one(&mut score);
println!("{score}");

The example makes the contract visible in code.

Practise this code in Playground ↗

Try it

Trace where &mut score begins and ends, and what it lets add_one do.

What success looks like: You can say why score can be read again on the last line, and what it holds.

Check your understanding

What does &mut grant temporarily?

Reveal the answer
  1. Shared read access
  2. Exclusive write access
  3. A copied value

Correct: Exclusive write access.

Go deeper

Common misconception: “&mut score moves score into add_one.”

It only lends exclusive access for the call. score stays the owner, which is why the last println! can read it; other access is rejected only while the mutable borrow is still in use.

  1. Mark the writer: score is declared mut, &mut score lends it to add_one, and *value += 1 writes through that reference until the call returns.
  2. Predict before checking: Predict what the last println! shows. Then store let edit = &mut score; and predict whether println!("{score}"); compiles before *edit += 1;.
  3. Compare with evidence: 7 prints. The early print gets E0502 while edit is still used later; move it after *edit += 1; and the program is valid.

Transfer challenge: Add fn reset(value: &mut i32) and call it after add_one. Then hold let first = &mut score; and let second = &mut score;, use first last, and read E0499.

Connections

  • Moves Across Functions: Parameters can take, borrow, or copy a value
  • Reborrowing: Lend access without consuming the original reference
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: Mutable Borrows (courseId "ownership", lessonId "mutable-borrows")
Link: https://crabrix.com/learn/ownership/mutable-borrows/
Goal: An &mut reference permits mutation without transferring ownership.
Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=ownership/mutable-borrows) 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.