---
title: "Borrowing and references in Rust"
slug: "borrowing-and-references"
description: "Use & to borrow a value and &mut to change it without moving it. Learn the rule of one mutable or many shared references, and fix two overlapping borrows."
published: "2026-12-03"
updated: "2026-12-03"
canonical: "https://aifree.dev/courses/learn-rust/borrowing-and-references"
---

# Borrowing and references in Rust

How do you use a value without taking ownership of it?

## Predict

```rust
fn len(s: &String) -> usize {
    s.len()
}

fn main() {
    let s = String::from("hello");
    let n = len(&s);
    println!("{s} has {n} bytes");
}
```

What does this program print?

- It does not compile, because s moved into len
- hello has 5 bytes
- has 5 bytes, with s empty

Answer: hello has 5 bytes — The function borrows s, so the owner keeps the value and can print it afterwards.

Output:

```
hello has 5 bytes
```

`&s` lends the String to `len` without giving it away. When the function ends, the reference is gone and `s` is still its owner.

## References borrow

A **reference** lets you use a value without owning it. `&s` creates one, and the type `&String` or `&str` accepts it. The owner keeps the value, and nothing is dropped when the reference goes out of scope. The Book calls creating a reference **borrowing**. To change what you borrowed, you need a mutable reference, `&mut s`, and the variable must be declared `mut`.

## A shared and a mutable borrow

```rust
fn main() {
    let mut name = String::from("Ferris");
    greet(&name);
    add_title(&mut name);
    greet(&name);
}

fn greet(who: &str) {
    println!("Hello, {who}!");
}

fn add_title(who: &mut String) {
    who.push_str(" the Crab");
}
```

Output:

```
Hello, Ferris!
Hello, Ferris the Crab!
```

`greet` takes `&str` and only reads. `add_title` takes `&mut String` and appends. The call sites show it with `&name` and `&mut name`.

Playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=13bccf359041685a517318653df722f3

## The borrowing rule

At any given time you can have **either one mutable reference or any number of immutable references** to a value. References must also always be valid, so one cannot outlive the value it points to. A reference lasts from where it is created to where it is last used, not to the end of the block.

## A mutable borrow after the shared ones end

```rust
fn main() {
    let mut s = String::from("hello");
    let r1 = &s;
    let r2 = &s;
    println!("{r1} and {r2}");
    let r3 = &mut s;
    r3.push_str(" world");
    println!("{r3}");
}
```

Output:

```
hello and hello
hello world
```

`r1` and `r2` are last used in the first `println!`. After that line they are over, so `&mut s` is allowed.

Playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=346087bd16a2d534c5d9e6d921f19603

## Fix it

```rust
fn main() {
    let mut s = String::from("hello");
    let r1 = &s;
    let r2 = &mut s;
    println!("{r1} {r2}");
}
```

This program does not compile. What is the bug?

- s must be declared with let mut s: &String
- r1 is still in use when &mut s is created, so a shared and a mutable borrow overlap
- push_str is missing on the String

Answer: r1 is still in use when &mut s is created, so a shared and a mutable borrow overlap — Use r1 before you create r2, or stop using it, so the borrows do not overlap.

Compiler message:

```
error[E0502]: cannot borrow `s` as mutable because it is also borrowed as immutable
```

Fixed:

```rust
fn main() {
    let mut s = String::from("hello");
    let r1 = &s;
    println!("{r1}");
    let r2 = &mut s;
    r2.push('!');
    println!("{r2}");
}
```

Output:

```
hello
hello!
```

## Say it back

In your own words: what is the borrowing rule, in one sentence?

Our sentence: At any moment a value can have either one mutable reference or any number of shared references, never both, and no reference may outlive the value.

## Mission (8 min)

Write a function that takes &mut String and appends text to it, and another that takes &str and prints it. Call both from main, then try holding a shared reference across the mutable call and read the error.

```sh
cargo new borrow
cd borrow
cargo run
```

## Common questions

### What is borrowing in Rust?

Borrowing means using a value through a reference instead of taking ownership of it. You write &value to lend it for reading, or &mut value to lend it for changing. The owner keeps the value, and it is not dropped when the reference ends, so you can keep using the original afterwards.

### Why can't I have two mutable references in Rust?

The borrowing rule allows either one mutable reference or any number of immutable ones at a time. Two mutable references to the same value could change it at once, so the compiler refuses them. A reference lasts until its last use, so you can take a new one after the first is no longer used.

### What is the difference between &str and &String?

Both are references to text. A &String borrows a whole String, while &str is a string slice that borrows a part or all of some text. A function that takes &str is more flexible, because you can pass it a reference to a String as well as a string literal.

## Sources

- https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html (read 2026-12-03)
- https://doc.rust-lang.org/book/ch04-03-slices.html (read 2026-12-03)

Measured with rustc 1.100.1 (play.rust-lang.org, stable, edition 2024) on 2026-12-03.
