Skip to content
Theory · Borrowing · lesson 3 of 11

Mutable references

A & reference only lets you read a value. Back to the book: sometimes a friend asks for it not to read but to make notes in the margins. That takes a separate permission, and the book comes back to you with the notes in it.

Let's see what happens if you try to add a character to a string through an ordinary reference:

Rust
fn add_mark(s: &String) {
    s.push('!'); // ✗
}

fn main() {
    let name = String::from("Anna");
    add_mark(&name);
    println!("{name}");
}
TEXT
error[E0596]: cannot borrow `*s` as mutable, as it is behind a `&` reference

The program does not build: s is a & reference, and you cannot change the string through it. The star in *s means "the string s looks at"; more on it below in this lesson. The compiler suggests a way out: make the parameter a mutable reference, &mut String.

A &mut reference

A mutable reference is a reference through which you can change the value. You create it with &mut name, and the type of a mutable reference to a String is written &mut String. Ownership does not move here either: the string stays with the calling code, only changed.

Rust
fn add_mark(s: &mut String) {
    s.push('!');
}

fn main() {
    let mut name = String::from("Anna");
    add_mark(&mut name);
    add_mark(&mut name);
    println!("{name}"); // Anna!!
}

Let's go through the example line by line:

  1. The parameter s has the type &mut String: the function gets the right to change someone else's string.
  2. let mut name declares a mutable variable. Without mut you cannot take a mutable reference to it.
  3. add_mark(&mut name) lends the string for writing, and s.push('!') adds the character to name itself.
  4. The second call adds one more !.
  5. println! prints Anna!!: the owner name sees both changes.

A &mut points to name itself, not to a copy of it, so it is name that changes. In the previous theme, add_mark took the string and handed it back as its result. With &mut there is nothing to hand back: the function changes the string in place, and name stays the owner the whole time.

Three muts in one program

For a function to change your string, you write the word mut three times:

  • let mut name: the owner allows the string to change;
  • s: &mut String: the function declares that it will change it;
  • add_mark(&mut name): the call explicitly lends the string for writing.

The last one is especially useful: the call line shows at once that the function may change name, with no need to look inside the function.

Writing through *

You call a method through &mut without a star, as through &: the dot finds the value on its own. But to replace the whole value — assign a new one or add to a number — you have to dereference the reference. Dereferencing is going from a reference to the value itself, and it is written with a star: *n.

Rust
fn add_one(n: &mut i32) {
    *n += 1;
}

fn main() {
    let mut score = 10;
    add_one(&mut score);
    add_one(&mut score);
    println!("{score}"); // 12
}

Let's go step by step:

  1. add_one gets a mutable reference to a number.
  2. *n += 1 follows the reference to score itself and adds one to it.
  3. Two calls increase score twice, and println! prints 12.

Without the star, n += 1 tries to add a number to the reference itself, and the compiler answers with error E0368. An i32 is a Copy type, but through &mut the function changes not a copy but the calling code's variable.

Here the book analogy falls short. In real life anyone holding the book can write in the margins. In Rust you can change a value through a reference only if the owner allowed it with the word mut in the declaration.

Interviewers like to ask this: "How does & differ from &mut?" The answer:

  • through & you can only read the value, through &mut you can also change it;
  • you can take a &mut only from a variable declared with mut;
  • neither reference takes ownership;
  • changes made through &mut are visible to the owner after the call, because the reference points to the value itself, not to a copy.

Common mistake

A mutable reference is taken from a variable declared without mut:

Rust
fn add_mark(s: &mut String) {
    s.push('!');
}

fn main() {
    let name = String::from("Anna");
    add_mark(&mut name); // ✗
    println!("{name}");
}
TEXT
error[E0596]: cannot borrow `name` as mutable, as it is not declared as mutable

The program does not compile: let without mut promises that the value will not change, and you cannot get around that promise through a reference. This is a compile error, not a panic, and the program never gets to run. The compiler suggests the fix: let mut name.

Key points

  • &mut name gives a mutable reference: through it a function changes someone else's value without taking ownership.
  • You can take a mutable reference only from a variable with mut, otherwise you get error E0596.
  • You call methods through &mut without a star, and write a new value through *n.
  • A &mut points to the variable itself, so the owner sees every change.