let mut x = 2;
let mut y = 2;
let x_addr = std::ptr::addr_of!(x);
let y_addr = std::ptr::addr_of!(y);
let r = if use_x { &mut x } else { &mut y };
(
std::ptr::eq(r, x_addr),
std::ptr::eq(r, y_addr),
)
Once you've created the mutable reference, you can no longer access the variable in any way. It's impossible to make this check without first storing the address of each variable before creating the mutable reference.