Partial Borrows in Rust
Sasha Pak, Richard Willie, Umang Mathur, Fabian Muehlboeck, Alex Potanin
Abstract
Rust ensures memory safety by requiring exclusive access to mutably borrowed data. However, a borrow can prevent access to more data than necessary. For example, a method with an &mut self parameter borrows the entire struct, even when the method uses only some of its fields. Calling the method can therefore conflict with an existing borrow of a field the method never uses, causing Rust to reject the program. Resolving such conflicts can require programmers to compromise useful abstractions, make non-local changes to their programs, or worse, resort to unsafe code. We introduce view types, a backward-compatible refinement of reference types that specifies which fields a reference may access. This allows methods to partially borrow a struct, accessing only the fields they need without blocking access to the rest. We establish the first fully mechanized foundation for Aeneas in Rocq. Along the way, we uncover and fix gaps in the original semantics and proofs. Building on this foundation, we formalize partial borrows and prove the extended borrow checking sound. We implement view types in rustc and extend the Aeneas interpreter to support partial borrows. Our evaluation shows how view types systematically resolve borrowing conflicts across method boundaries at no runtime cost.
Create a lesson
Related papers
Verified Implementation of the Next-gen Borrow Checking Algorithm for Rust via Abstract Interpretation
Shared title terminology · Jinhua Wu, Yuting Wang, Zhemin Chen
C-to-Rust Translation Validation at the LLVM IR Level via AI-Assisted Refinement Proof
Shared title terminology · Eunsu Kim, Sukyoung Ryu, Jaemin Hong
Crustify: Repository-Level Generation of Safe Rust Bindings with LLM Agents and Coverage-Guided Testing
Shared title terminology · Marius Momeu, Marius Arvinte, Cory Cornelius et al.
Modular Responsiveness Verification of Rust Async Runtimes
Shared title terminology · Yanze Li, Ivan Beschastnikh, Alexander J. Summers
A Complete, Formal Semantics for Rust Source Code
Shared title terminology · Daniel Drodt