I will follow the documentation at rust-lang.org to get a better grip on ownership. The tutorial breaks down the concept in three chunks:
-
Ownership
-
References and Borrowing
-
Lifetimes
In this post will summarize my general understanding of ownership.
tl;dr: Keep in mind the ownership concept when reassigning variables with existing bindings. If not a primitive, an error will occur at compile time.
Point #1:
Basically that variable bindings have ownership of what the variable is bound to. The example given is that:
Ok, to have “ownership” means that v basically owns vec![1,2,3]. Thus if we decide set a new variable, y = v, then v no longer has ownership, and will result in a compile time error. This addresses the idea that Rust gaurantees that bindings to resources are unique.
This is not allowed:
The phrase used is “move”, as in the error will be related to the attempt to accessing, the use of the “moved” value v.
Point #2:
The trait, Copy, is introduced, where a trait is feature that adds incremental behavior.
Basic concept is that if its a primitive type that is bound, then the Copy trait comes in to play and we can reassign ownership and still address the original the original variable.
Ok, this is fine now and will compile as 5 is a primitive. Essentially the Copy trait invokes a deep copy, and y does not reference the object pointed to by v.