Rust Polonius Borrow Checker

What is the Borrow Checker?

Rust’s borrow checker is the compile-time system that enforces ownership and borrowing rules. It guarantees memory safety (no dangling pointers, no data races, no use-after-free) without a garbage collector.

What is Polonius?

Polonius is a more precise version of the borrow checker. It is named after the Shakespeare character who said “Neither a borrower nor a lender be.”

Key improvement: It accepts more safe programs that the current Non-Lexical Lifetimes (NLL) analysis rejects, while keeping the exact same safety guarantees.

Why it matters

Classic example that fails with the normal borrow checker but works with Polonius:

Rust

fn get_or_insert(map: &mut HashMap<u32, String>, key: u32) -> &mut String {
    match map.get_mut(&key) {
        Some(value) => value,
        None => {
            map.insert(key, String::new());
            map.get_mut(&key).unwrap()
        }
    }
}

How to enable it (Nightly only)

Bash

# One-off
rustc +nightly -Zpolonius=next file.rs

# With Cargo
RUSTFLAGS="-Zpolonius=next" cargo +nightly build

Permanent for a project (.cargo/config.toml):

toml

[build]
rustflags = ["-Zpolonius=next"]

Algorithm (high level)

  • NLL tracks lifetimes as sets of program points.
  • Polonius tracks origins as sets of loans and uses subset relations between them.
  • It propagates loans more precisely along the control-flow graph only when the containing origins are live.
  • This gives better flow sensitivity (especially for conditional returns and reborrows).

Current Status (July 2026)

  • Still experimental / nightly-only
  • Not enabled by default
  • Work is ongoing to stabilize “Polonius alpha”