Implementation mutable reference question

impl MyThing{
    fn test_borrow(&mut self){
        println!("OwO {}",;

  let a = MyThing{...};

In this code, a can't be mutated so the compiler rightly complained to me.

impl Drop for MyThing{
    fn drop(&mut self){
        println!("Dropping {}",;

But if I implement Drop for MyThing, the funcitno signature uses "&mut". The compiler didn't complain, and the program actually ran to finished and drop was called.

Am I missing something? Is Drop special?

Drop is a bit special. But remember that the owner decides about mutability.

You can change your example to:

let a = MyThing{...};
    let mut b = a;

And it's ok. And even this!

let a = MyThing{...};

(Hint: the variable a no longer owns the MyThing value, it becomes a temporary)

1 Like

I see how drop is special, and we are not allowed to directly call it anyway. That makes sense. Thank you for your explanation.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.