liveBook · Manning

Borrow checker

As explained in Rust in Action: Systems programming concepts and techniques

The borrow checker is the Rust compiler component that verifies ownership, borrowing, and lifetime rules to prevent invalid memory access.

The borrow checker checks whether access to data is legal. By enforcing ownership, borrowing, and lifetime rules, it helps prevent invalid access before a Rust program runs. section 4.0 section 4.6

Three related ideas help explain its decisions:

  • Ownership: an owner is responsible for cleaning up a value when its lifetime ends. section 4.6
  • Lifetime: the period when accessing a value is valid. A variable can remain in scope even after its value is no longer available. section 4.0 section 4.1.1
  • Borrowing: access to a value owned elsewhere. Borrows can be read-only or read-write; only one read-write borrow can exist at a time. section 4.0 section 4.6

A satellite used after a move

A CubeSat value is passed to check_status(sat_a), moving ownership into the function. When the function returns, it drops the value. A second call using sat_a fails, even though the variable name is still in scope. section 4.1.1

Listing 4.3 Checking the status of our integer-based CubeSats
 #[derive(Debug)]                     #1
 struct CubeSat {
   id: u64,
 }
 
 #[derive(Debug)]
 enum StatusMessage {
   Ok,
 }
 
 fn check_status(
   sat_id: CubeSat
 ) -> StatusMessage {                 #2
   StatusMessage::Ok
 }
 
 fn main() {
   let sat_a = CubeSat { id: 0 };     #3
   let sat_b = CubeSat { id: 1 };     #3
   let sat_c = CubeSat { id: 2 };     #3
 
   let a_status = check_status(sat_a);
   let b_status = check_status(sat_b);
   let c_status = check_status(sat_c);
   println!("a: {:?}, b: {:?}, c: {:?}", a_status, b_status, c_status);
 
   // "waiting" ...
   let a_status = check_status(sat_a);
   let b_status = check_status(sat_b);
   let c_status = check_status(sat_c);
   println!("a: {:?}, b: {:?}, c: {:?}", a_status, b_status, c_status);
 }

Follow the two status checks in main: the second set attempts to reuse satellite values moved by the first. section 4.1.1

error[E0382]: use of moved value: `sat_a`
  --> code/ch4-check-sats-2.rs:26:31
   |
 |   let a_status = check_status(sat_a);
   |                               ----- value moved here
...
 |   let a_status = check_status(sat_a);
   |                               ^^^^^ value used here after move
   |
   = note: move occurs because `sat_a` has type `CubeSat`,
   = which does not implement the `Copy` trait
 
...      #1
 
error: aborting due to 3 previous errors

The compiler identifies both the move and the later use of sat_a, reporting error E0382. section 4.1.1

When a program fails a borrow check, consider changing its design: borrow a value instead of moving it, shorten how long values are kept, or duplicate a value with Clone or Copy. Reference counting with Rc<T> and interior mutability offer other ways to arrange access; choose based on the program’s needs. section 4.0 section 4.6

An unsafe block does not bypass the borrow checker. It signals that the compiler cannot guarantee memory safety by itself, leaving the programmer responsible for maintaining the program’s integrity. section 5.1

How Rust in Action: Systems programming concepts and techniques covers it

How deep it goes
Explains ownership, lifetimes, borrowing, and compiler errors, and summarizes several design tactics for resolving conflicts.
Examples
Traces repeated satellite status checks through an ownership move and a use-after-move compiler error.
What you can do after
Diagnose and debug borrow-checking errors by tracing ownership and considering alternative access or duplication strategies.
How it connects
Connects borrow checking to ownership, lifetimes, borrowing, reference counting, interior mutability, and unsafe Rust.

Also explained in

Compare how 4 books explain borrow checker →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Rust in Action: Systems programming concepts and techniques. The section links point to the parts of the book it draws on.