Borrow Checker (Rust)
The borrow checker is the part of the Rust compiler that enforces the ownership and borrowing rules at compile time, guaranteeing memory safety and freedom from data races without a garbage collector.
Rust
The borrow checker statically verifies that references never outlive the data they point to and that you never have a mutable borrow alongside any other borrow of the same value. If your code could cause a dangling pointer, double free, or data race, it simply won't compile.
This is what lets Rust promise 'fearless concurrency' and memory safety with no runtime cost — all the checking happens before the program runs. The price is the famous learning curve: the borrow checker rejects programs that would be fine in C, forcing you to make ownership explicit.
Modern Rust uses 'non-lexical lifetimes', so a borrow ends at its last use rather than the end of the block, which accepts far more correct programs. When fighting the borrow checker, the usual fixes are: shorten a borrow's scope, clone the data, restructure ownership, or use a smart pointer like Rc/RefCell.
Examples
Rust
fn main() {
let mut s = String::from("hi");
let r1 = &s; // ok: shared borrow
println!("{}", r1); // last use of r1 here
let r2 = &mut s; // ok now: r1 no longer used
r2.push('!');
println!("{}", s);
}Output:
hi
hi!