5 ms·
I don't understand your comment. `drop` takes a *mutable* reference. But by default, references in Rust are immutable.
by skywal_l 1y ago
I don't understand your comment. `drop` takes a *mutable* reference. But by default, references in Rust are immutable.
- littlestymaar 1y agoshared references are immutable in Rust, mut references and shared references are both references.
- Ygg2 1y agoKibwen is saying that there was an idea to have `fn drop(&mut x)` from Drop trait become `fn drop_own(&own x)`. Then `&own` would do mutation and drop the owner of the reference. A regular &mut reference shouldn't do that outside of `drop(&mut x)`.
- codedokode 1y agoDrop takes an object itself ("owned reference"), as I remember. Mutable ref allows reading/writing but not destroying or passing the ownership. Owner = can read/write/destroy Mutable ref = can read/write Immutable ref = can only read, guarantered not to change
- steveklabnik 1y agohttps://doc.rust-lang.org/stable/std/ops/trait.Drop.html#tymethod.drop https://doc.rust-lang.org/stable/std/ops/trait.Drop.html#tym...
- codedokode 1y agoThat's the destructor function, that is written by you and called by Rust before actually destroying something. The function that you want to look at is [1]. If you read the docs at your link it even says: > This method is called implicitly when the value goes out of scope, and cannot be called explicitly (this is compiler error E0040). > However, the mem::drop function in the prelude can be used to call the argument’s Drop implementation. By the way the implementation of the function drop is just an empty function [2]; that's enough as local variables are destroyed on function return. Mutable reference is a "borrow" which means you take a value from an owner and promise to return it back, and you cannot destroy a thing that you must return. [1] https://doc.rust-lang.org/std/mem/fn.drop.html https://doc.rust-lang.org/std/mem/fn.drop.html [2] https://doc.rust-lang.org/src/core/mem/mod.rs.html#957 https://doc.rust-lang.org/src/core/mem/mod.rs.html#957
- steveklabnik 1y agoThe drop function being talked about here is the one I pointed to, not the one you pointed to. The Drop trait is built into the language (as a lang item), std::mem::drop is just a regular old function.
- codedokode 1y agoThe drop that you mention doesn't free memory, as I understand, it is called before actually destroying object's memory.
- steveklabnik 1y agoNot inherently, sure. But ultimately this is far afield of what I was trying to say, which is that the signature being discussed of being changed from &mut T to &owned T is the one from the Drop trait. That’s it.