On this page
article
Ownership
Ownership
What Is Ownership?
- Ownership in simple terms means to have possession of something.
- Let’s look at a real-life analogy to explain this concept. If something belongs to you, you say “It’s mine”.
- Similarly, in Rust, variable bindings can have ownership of what they are bound to.
Three Rules of Ownership
The following are three rules of ownership:
Rule 1
Each value has a variable binding called its owner.
Rule 2
There can only be one owner at a time.
Rule 3
- When an owner goes out of scope, it does not remain accessible.
- When the variable goes out of scope, Rust calls function drop automatically at the closing curly bracket (to deallocate the memory).
- When the variable goes out of scope, Rust calls function drop automatically at the closing curly bracket (to deallocate the memory).
output
- When using the assignment, two situations happen:
- The value gets copied
- The value gets moved
Last updated a year ago.