It is my understanding that when a variable whose type implements Drop
goes out of scope, a call to the fn drop(&mut self)
function is inserted, and passed a newly-created mutable reference to the variable going out of scope.
However, how is that possible in cases where the variable was immutably bound, and it would be illegal to borrow it mutably? Here's an example of what I'm talking about:
fn main() {
let x = vec![1, 2, 3];
let y = &mut x;
}
Which produces the following error: cannot borrow immutable local variable x
as mutable as expected.
Something similar must happen when x
would be getting dropped, because drop
expects a mutable reference.