by kibwen · 1y ago · 0 comments
This isn't quite true. While Rust doesn't currently support this, people have proposed the concept of `&own` references that take ownership of the object and free it when the reference goes out of scope (consider that Rust's standard destructor signature, `fn drop(&mut)`, should probably take one of these hypothetical owning references). I addition, I believe that languages with typestate can cause types to change as a result of function calls, although I don't quite understand the details.
I believe so, yes. Currently the only way to transfer ownership is by-value, and LLVM might optimize away the memcpy, but also it might not.
Owner = can read/write/destroy
Mutable ref = can read/write
Immutable ref = can only read, guarantered not to change
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.
Do you have any specific languages?
let x: u8;
I can't actually do anything with this variable: foo(x); // error
Except I can apply the initialization operator: x = 42;
And now I can do things with this variable as usual.But consider that the type of x hasn't changed as a result of the initialization. Beforehand it was a u8, and afterward it was still a u8, and yet something about the state of the variable changed my ability to use it in various contexts. I believe that typestate is something like this, generalized to allow variables to flow through states (like a compile-time state machine) to ensure that things happen in the correct order.
How can you free something if it's not allocated
What I meant was, creating a box creates a new allocation, whereas my understanding of &own would take over an existing allocation.