# What is the borrow checker, and why does it reject my code?
Source: https://ruststudio.org/community/t/1/what-is-the-borrow-checker-and-why-does-it-reject-my-code
Author: Ruststudio Team (staff) | Published: 2026-12-05 | Updated: 2026-12-05 | Checked with: rustc 1.93.0, Windows 11
Category: Help and questions | Kind: FAQ | Tags: borrow-checker, basics, errors | Views: 0

## Short answer
The borrow checker is the part of the Rust compiler that enforces ownership rules at compile time. A value has one owner, and at any moment it has either any number of shared references or exactly one mutable reference. Most rejections are fixed by ending a borrow sooner, copying, or splitting the data.

## Answer
## Answer
The borrow checker is a compile-time check, not a runtime one: when it rejects code, the program would have had a memory bug (a dangling reference, or two parts of the program changing the same data at once) if the compiler had let it through.

It enforces three rules:

1. Every value has exactly one owner. Assigning a value to another variable, or passing it to a function by value, moves it; the old name can no longer be used.
2. At any moment you can have **either** any number of shared references (`&T`) **or** one mutable reference (`&mut T`) to a value, never both at the same time.
3. A reference can never outlive the value it points to.

The compiler error tells you which rule was broken and points at the lines involved. The four messages below are the ones people meet first. They were produced by rustc 1.93.0 on Windows 11, shortened where long.

| Code | Message (start) | Rule broken |
|---|---|---|
| E0382 | `borrow of moved value: name` | 1: the value was moved, then used again |
| E0499 | `cannot borrow v as mutable more than once at a time` | 2: two `&mut` at once |
| E0502 | `cannot borrow scores as mutable because it is also borrowed as immutable` | 2: `&` and `&mut` at once |
| E0505 | `cannot move out of s because it is borrowed` | 3: the value was moved or dropped while a reference to it is still used later |

This is the most common one:

```rust title=src/main.rs
fn main() {
    let mut scores = vec![10, 20, 30];
    let first = &scores[0];   // a shared borrow starts here...
    scores.push(40);          // ...a mutable borrow is needed here (push may move the data)
    println!("{first}");      // ...and the shared borrow is still used here
}
```

```text
error[E0502]: cannot borrow `scores` as mutable because it is also borrowed as immutable
 --> src/main.rs:4:5
  |
3 |     let first = &scores[0];
  |                  ------ immutable borrow occurs here
4 |     scores.push(40);
  |     ^^^^^^^^^^^^^^^ mutable borrow occurs here
5 |     println!("{first}");
  |                ----- immutable borrow later used here
```

Here `push` may need to allocate a bigger buffer and move all elements, which would leave `first` pointing at freed memory. The compiler cannot know that `push` will not do that, so it refuses. The answers below show three different ways to fix it; which one fits depends on what the code is really doing.

Each error code has a long explanation: run `rustc --explain E0502`.

## Common mistakes
- Fighting the checker with `clone()` everywhere. A clone is a valid fix when you need an independent copy, but cloning only to silence an error can hide a design problem.
- Reaching for `unsafe` or raw pointers to get around an error. The rules exist because the code would be unsound; restructure instead.
- Reading only the first line of the error. The labels under the code lines say where the borrow starts and where it is used later, and that is where the fix goes.

## Related questions
### Does the borrow checker slow my program down?
No. It runs only while compiling and produces no code, so there is no runtime cost.

### What does the lifetime error "borrowed value does not live long enough" mean?
That is rule 3: a reference was kept after the value it points to was dropped, for example by returning a reference to a local variable. Return the value itself (or a copy) instead, or make the owner live longer than the reference.

## Sources
- The Rust Book, "Understanding Ownership": https://doc.rust-lang.org/book/ch04-00-understanding-ownership.html
- The Rust Book, "References and Borrowing": https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html
- Error code index: https://doc.rust-lang.org/error_codes/E0502.html

## Answers (3)

### Answer 1 by Ruststudio Team (staff), 2026-12-05, 0 votes
## Approach 1: end the borrow before you change the data
Use this when you only need the value at one moment. Copy the value out (or finish using the reference) before the mutation, and the shared borrow is over by the time `push` runs.

```rust
fn main() {
    let mut scores = vec![10, 20, 30];
    let first = scores[0];     // i32 is Copy: this copies the number, it does not borrow
    scores.push(40);
    println!("{first} {scores:?}");
}
```

```text
10 [10, 20, 30, 40]
```

Trade-off: it costs nothing for small `Copy` types such as numbers. It does not help when you really need the reference to stay valid across the mutation; for that, see approach 2 or 3.

### Answer 2 by Ruststudio Team (staff), 2026-12-05, 0 votes
## Approach 2: keep an independent copy with `clone`
Use this when the data is not `Copy` (a `String`, a struct) and you need the value to outlive a change or a move of the original.

```rust
fn main() {
    let mut scores = vec![10, 20, 30];
    let first = scores.first().copied();   // Option<i32>, no borrow left behind
    scores.push(40);
    println!("{first:?} {scores:?}");

    let names = vec![String::from("Ada"), String::from("Bo")];
    let kept = names[0].clone();           // an owned String, independent of `names`
    drop(names);
    println!("{kept}");
}
```

```text
Some(10) [10, 20, 30, 40]
Ada
```

Trade-off: a clone of a `String` or `Vec` allocates and copies, which is fine for occasional use and costly inside a hot loop. Cloning does not change meaning: you now have two values, and changes to one do not show in the other.

### Answer 3 by Ruststudio Team (staff), 2026-12-05, 0 votes
## Approach 3: split the data, or use indices
Use this when two parts of the same collection must be changed together. `split_at_mut` hands out two non-overlapping mutable slices, which the compiler can prove are disjoint. Indices avoid long-lived references altogether.

```rust
fn main() {
    let mut v = vec![1, 2, 3, 4];
    let (left, right) = v.split_at_mut(2);   // two &mut slices that cannot overlap
    left[0] += right[1];
    right[0] += left[1];
    println!("{v:?}");

    let n = v.len();
    for i in 0..n {
        let next = v[(i + 1) % n];           // read by index, then write by index
        v[i] += next;
    }
    println!("{v:?}");
}
```

```text
[5, 2, 5, 4]
[7, 7, 9, 11]
```

Trade-off: no copying and no extra allocation, but the code has to be arranged around the split. For structs, borrowing two different fields at the same time also works, because the compiler tracks fields separately.
