What logic does the borrow checker use to analyze references derived from unsafe code?
⚓ Rust 📅 2025-07-28 👤 surdeus 👁️ 12Hello, world! I'm a long-time C++ user, starting to learn Rust because a project I'll be working on in the fall is implemented largely in Rust. I've been reading through The Book over the past several days, and very much enjoying it so far. While reading the section on unsafe code, a question occurred to me: how does the borrow checker analyze references returned by unsafe code?
As a motivating example, consider slice::split_at_mut
This code compiles and runs as one might expect:
fn main() {
let mut data = [0, 1, 2, 3];
let (left, right) = data.split_at_mut(2);
println!("Left: {left:?}. Right: {right:?}");
let data_ref = &data;
println!("Data_ref: {data_ref:?}");
// Uncommenting this line results in a borrow checker error:
// "error[E0502]: cannot borrow `data` as immutable because it is also borrowed as mutable"
// println!("Left again: {left:?}.");
// Uncommenting this line *also* results in a borrow checker error.
// println!("Right again: {right:?}.");
}
As the comments note, the borrow checker will correctly throw an error if a reference to either left or right is used after data_ref is borrowed. What logic does the borrow checker use to infer that both references returned from slice::split_at_mut are borrows of data?
3 posts - 3 participants
🏷️ Rust_feed