Learning about ownership and moving
When we instantiate a struct, we create an instance. Imagine a struct as being like a template; an instance is created in the memory based on the template and filled with appropriate data.
An instance in Rust has a scope; it is created in a function and gets returned. Here is an example:
fn something() -> User {
let user = User::find(...).unwrap();
user
}
let user = something()
If an instance is not returned, then it's removed from memory because it's not used anymore. In this example, the user
instance will be removed by the end of the function:
fn something() {
let user = User::find(...).unwrap();
...
}
We can say that an instance has a scope, as mentioned previously. Any resources created inside a scope will be destroyed by the end of the scope in the reverse order of their creation.
We can also create...