fn main() {
let s = String::from("Hello");
drop(&s);
println!("{}", s); // prints `Hello`
}
Why does this code compile and work? More specifically, I don't understand the following:
- Why I can use
s
inprintln!
after I dropped it? Doesn'tdrop
free the underlying resource making it unusable? - According to the
Drop
trait docs, the functiondrop
accepts a mutable reference, which doesn't make any sense to me - shouldn't it accept the value itself instead of a reference? - Let's assume it accepts a mutable reference — in the code above I pass an immutable reference - so why does it still work?