Summary

The Rust borrow checker is the part of the compiler that checks rules such as “one owner per value, a reference cannot outlive its value, and only one mutable reference at a time” at compile time. C++ keeps these rules through RAII and developer habit, while Rust enforces them in the compiler. That is the biggest difference.

One-line answer: The Rust borrow checker is the compile-time step of the compiler that enforces ownership, reference and lifetime rules.

What is the Rust borrow checker, and what does it check?

One-line answer: It checks three things at compile time: ownership and move, many &T or one &mut T, and reference lifetimes.

The rules from The Rust Book, chapter 4:

  • Each value has an owner.
  • There can be only one owner at a time.
  • When the owner goes out of scope, the value is dropped.
  • At any time you can have either one mutable reference or any number of immutable references.
  • References must always be valid.

Rust ownership and borrowing rules at a glance

How does it catch a move? (E0382)

This code moves s1 into s2 and then uses s1 again. (rustc 1.99.1, --edition 2021, run on 2026-10-21)

fn main() {
    let s1 = String::from("hello");
    let s2 = s1;            // 소유권이 s2로 이동
    println!("{s1} {s2}");  // s1은 더 이상 쓸 수 없음
}
$ rustc --edition 2021 move.rs
error[E0382]: borrow of moved value: `s1`
 --> move.rs:4:16
  |
2 |     let s1 = String::from("hello");
  |         -- move occurs because `s1` has type `String`, which does not implement the `Copy` trait
3 |     let s2 = s1;            // 소유권이 s2로 이동
  |              -- value moved here
4 |     println!("{s1} {s2}");  // s1은 더 이상 쓸 수 없음
  |                ^^ value borrowed here after move
  |
help: consider cloning the value if the performance cost is acceptable
  |
3 |     let s2 = s1.clone();            // 소유권이 s2로 이동
  |                ++++++++

error: aborting due to 1 previous error

The moved s1 can no longer be used. One of the fixes the compiler suggests is clone().

How does it check shared vs mutable references? (E0502)

fn main() {
    let mut v = vec![1, 2, 3];
    let first = &v[0];      // 공유 참조
    v.push(4);              // 가변 빌림이 필요
    println!("{first}");
}
$ rustc --edition 2021 borrow.rs
error[E0502]: cannot borrow `v` as mutable because it is also borrowed as immutable
 --> borrow.rs:4:5
  |
3 |     let first = &v[0];      // 공유 참조
  |                  - immutable borrow occurs here
4 |     v.push(4);              // 가변 빌림이 필요
  |     ^^^^^^^^^ mutable borrow occurs here
5 |     println!("{first}");
  |                ----- immutable borrow later used here

error: aborting due to 1 previous error

The shared reference first, created with &v[0], is still alive while v.push(4) needs a mutable reference (&mut), so this does not compile.

What about lifetimes?

A reference must not outlive the value it points to. The full example is in the next question (E0597).

How is it different from C++?

One-line answer: The same mistake, using a pointer after its scope ended, compiles in C++ and shows up at run time, while Rust rejects it at compile time with E0597.

The C++ code and results

This code reads, outside the inner scope, a pointer to a std::string declared inside it.

#include <iostream>
#include <string>

int main() {
    const std::string* r;
    {
        std::string s = "this string is longer than fifteen chars";
        r = &s;
    }                       // s가 여기서 소멸됨
    std::cout << *r << '\n';
}

Compiled with g++ 14.2.0 and -std=c++17 -Wall -Wextra, it compiles with exit code 0 and a -Wdangling-pointer= warning. Whether such a warning appears depends on the case and the flags, so it is not guaranteed.

$ g++ -std=c++17 -Wall -Wextra dangle.cpp -o dangle
dangle.cpp: In function ‘int main()’:
dangle.cpp:10:24: warning: using dangling pointer ‘r’ to ‘s’ [-Wdangling-pointer=]
   10 |     std::cout << *r << '\n';
      |                        ^~~~
dangle.cpp:7:21: note: ‘s’ declared here
    7 |         std::string s = "this string is longer than fifteen chars";
      |                     ^

Building the same code with -g -fsanitize=address and running it catches the error at run time. The report continues; these are its first 3 lines.

$ g++ -std=c++17 -g -fsanitize=address dangle.cpp -o dangle-asan && ./dangle-asan
=================================================================
==1695629==ERROR: AddressSanitizer: heap-use-after-free on address 0x504000000010 at pc 0x7f27b6c7a5a6 bp 0x7ffecb263480 sp 0x7ffecb262c40
READ of size 40 at 0x504000000010 thread T0

This run-time detection is covered in the AddressSanitizer post. valgrind can also catch similar run-time errors; see the valgrind vs ASan post for the difference.

The Rust code and result

fn main() {
    let r;
    {
        let s = String::from("hi");
        r = &s;
    }                       // s가 여기서 해제됨
    println!("{r}");
}
$ rustc --edition 2021 dangle.rs
error[E0597]: `s` does not live long enough
 --> dangle.rs:5:13
  |
4 |         let s = String::from("hi");
  |             - binding `s` declared here
5 |         r = &s;
  |             ^^ borrowed value does not live long enough
6 |     }                       // s가 여기서 해제됨
  |     - `s` dropped here while still borrowed
7 |     println!("{r}");
  |                - borrow later used here

error: aborting due to 1 previous error

In short, C++ detects it at run time (ASan), while Rust refuses to build it.

C++ run-time detection vs Rust compile-time rejection

Does the borrow checker have bugs too?

One-line answer: Yes. It is software, and an old soundness bug has been reported.

This is a compiler implementation bug, and fixing it makes the compiler reject code that it should have rejected all along. You can read the discussion in the Hacker News thread (posted around 2026-10-13).

Where should a C++ developer start?

One-line answer: Map it to what you already know, in this order: move, then & vs &mut, then scopes, then clone().

  • Move: connect it to C++ move semantics (copy/move constructors). In Rust, a move invalidates the source (E0382 above).
  • Ownership and cleanup at scope end: this connects to RAII.
  • &T vs &mut T: connect it to E0502 above.
  • Scope and lifetime: connect it to E0597 above.
  • clone(): the fix the compiler itself suggested in E0382.

What are the frequently asked questions?

One-line answer: Three short answers follow.

Does unsafe turn the checker off?

No. unsafe only allows a few extra operations, such as dereferencing raw pointers. Source: The Rust Book, chapter 20, Unsafe Rust.

What if my code does not compile because of the borrow checker?

Options include restructuring scopes, clone(), and Rc/RefCell.

Does C++ have similar tools?

Yes: compiler warnings such as -Wdangling-pointer and sanitizers such as ASan. They help, but they do not guarantee anything.

What are the sources?

Sources