There is a great example of Rust's move semantics documented here: Rust Move Semantics on the Rust By Example website.
I have a basic understanding of both cases demonstrated. The first being how a primitive can have a new alias and the original can still be used because the end result is a copy seeing as i32
utilizes the Copy
trait. This makes good sense to me.
Additionally, for many good reasons the second example makes sense in terms of having multiple aliases that refer to an i32
on the heap. Rust enforces ownership rules and therefore the original alias cannot be used now that a new binding has been created. This helps prevent data-races, double frees, etc.
But it would seem there is a third case which is not talked about. How does Rust implement moves of stack allocated structs that do not implement the Copy
trait? This is illustrated with the following code:
#[derive(Debug)]
struct Employee{
age: i32,
}
fn do_something(m: Employee){
println!("{:?}", m);
}
fn main() {
let x = Employee {
age: 25,
};
do_something(x);
//compiler error below because x has moved
do_something(x);
}
This I know: In the case above, Rust will allocate the Employee
on the stack. The above struct does not implement the Copy
trait and therefore will not be copied when assigned to a new alias. This is very confusing to me because if the Employee
struct is allocated on the stack and also does not implement the Copy
trait where/how does it move? Does it physically get moved to do_something()
's stack frame?
Any help is appreciated in explaining this conundrum.
Employee
struct less complex and at least to remove the lifetime.struct Employee { age: i32 }
would be enough, for example. – Peipus