-
Notifications
You must be signed in to change notification settings - Fork 0
Copy‐able Values
- Some types of values like numbers, booleans, etc are going to appear to break the rules of ownership!!
I got an error here because I am trying to make use of a moved value. Initially, this account value is assigned to account here: let account = Account::new(1, String::from("me"));
, then we moved that value into other account here: let other_account = account;
and then we tried to use account
that no longer contains ownership of the value anymore here: println!("{:#?}", account);
., this is classic ownership in action.
Now I will change it up a bit.
fn main() {
let num = 5;
let other_num = num;
println!("{} {}", num, other_num);
}
This does not give me any errors at all, even though its almost identical to the code above it making use of account value that resulted in an error. So this is rule 7 in action. So what is going on behind the scenes here? Whenever you have one of these values that you do something with that you think would cause a move, like assigning to another binding, passing it off to a function, or assigning it to a field on a struct, or putting it into a vector. With these kinds of values, the value is copied instead, which means they just behave like a value in any other programming language:
So lets illustrate whats going on behind the scenes: