- Views: 1
- Report Article
- Articles
- Writing
- Article Marketing
What is Rust Ownership?
Posted: Oct 29, 2020
What is Rust Ownership?Ownership concept Memory and Allocation
- In Rust, data can be stored either in stack or heap memory.
- Stack Memory
- Heap Memory
It stores values in the order it get them and removes the values in the opposite order. Referred to as Last In, First Out (LIFO). Stack: Think of a stack of plates
When you add more plates,you put them on the top of the pile, and when you need a plate, you take one off the top. Adding or removing plates from the middle or bottom wouldn't work as well.
- Adding data is called pushing onto the stack,
- Removing data is called popping off the stack.
- Stack memory is an organized memory.
- It is faster than the heap memory because of the way it accesses the memory.
- All data stored on the stack must have a known, fixed size
Stack Push and Pop Example:
Stack: in action
Stack: How it works?
When code calls a function, the values passed into the function (including, potentially, pointers to data on the heap) and the function’s local variables get pushed onto the stack.
When the function is over, those values get popped off the stack.
- The Heap is Less Organized.
- Requested from OS.
- Slower.
- Follow pointer.
- large amount...take time to mange data in the heap.
- Data with a size that is unknown at compile time or a size that might change must be stored on the heap.
Heap: Allocation
- When you put data on the heap, you ask for some amount of space from OS.
- The operating system finds an empty spot in the heap that is big enough, marks it as being in use, and returns a pointer, which is the address of that location.
Why pushing to the stack is faster than allocating on the heap?
Because the operating system never has to search for place to store new data;that location is always at the top of the stack.
Why accessing data in the heap is slower than on the stack?
Allocating space on the heap requires more work,because you ask for some amount of space to operating system every time. OS has to follow the pointer every time.
Ownership- All programs have to manage the way they use a computer’s memory while running.
- Some languages have garbage collection that constantly looks for no longer used memory as the program runs.
- In other languages, the programmer must explicitly allocate and free the memory.
- Rust uses a third approach wherein memory is managed through a system of ownership.
- Ownership is managed with a set of rules that the compiler checks at compile time.
- None of the ownership features slow down your program while it’s running.
In simple words for understanding purpose……
- Ownership is the transfer of currently possessed entity to another party which causes the previous owner to no longer have access to the object that is being transferred.
- In Rust, there are very clear rules about which piece of code owns a resource.
- In the simplest case, it’s the block of code that created the object representing the resource.
- At the end of the block the object is destroyed and the resource is released.
- Example
fn main()
{// s is not valid here, it’s not yet declared
let s = "hello";
// s is valid from this point forward
// do stuff with s
println!("{}", s);
}// this scope is now over, and s is no longer valid
In the above example the letter or variable "s" is the owner of the word "hello" and is valid from the point of declaration after the start of the parenthesis "{" and remains valid until the end of the parenthesis "}".
Why Ownership
- Keeping track of what parts of code are using what data on the heap, minimizing the amount of duplicate data on the heap, and cleaning up unused data on the heap so you don’t run out of space are all problems that ownership addresses.
- All primitive data types (integers, booleans, string literals) and pointers (address of heap data) are stored on stack whereas for more complicated data types we have heap.
let a = "Hello world!";
- p>
In the above example the variable i.e "a" is also the owner of the value "Hello world!".
Rule # 2
There can only be one owner at a time
Example
let a = String::from("Hello"); –> here variable "a" is the owner
let b = a; –> here the value of "a" is moved to variable "b" which now becomes the owner of "Hello"
Considering both variables "a" and "b" are within the same scope.
Rule # 3
When the owner goes out of scope, so does the value
Example
fn main() {
{ –> "a" is not valid here, it’s not yet declared
let a = "Hello"; –> "a" is valid from this point forward
- do stuff with "a"
} –> this scope is now over and "a" is no longer valid
}
For more detail click
About the Author
Mansoor Ahmed Chemical Engineer,Web developer
Rate this Article
Leave a Comment