DEV Community

Cover image for What is Rust Ownership?
Mansoor Ahmed
Mansoor Ahmed

Posted on

What is Rust Ownership?

Ownership concept
Memory and Allocation
Data can be stored either in stack or heap memory in Rust.
Memory Types
Stack Memory
Heap Memory
Stack and Heap
Stack and Heap are parts of memory to be used at runtime, but they are structured in different ways.
Stack
Stack rust ownership
It collects 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
Stack rust ownership
When we put more plates,we add them on the top of the pile, and when we required a plate, we take one off the top. Putting 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 more faster than the heap memory due to the way it accesses the memory.
All data gathered on the stack must have a known, fixed size
Stack Push and Pop Example:

Stack rust ownership
Stack rust ownership
Stack: in action

Stack rust ownership
Stack: How it works?

The code when calls a function, the values moves 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 end, those values get popped off the stack.

Heap
The Heap is Less Organized.
Requested from OS.
Slower.
Follow pointer.
Huge amount…take time to mange data in the heap.
The Data with a size that is unknown at compile time or a size that might change must be kept on the heap.
Heap rust ownership
Heap: Allocation

When we put data on the heap, we say for some amount of space from OS.

The operating system gets 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 more fast than allocating on the heap ?

Because the operating system has not to search for place to store new data;that location is always at the top of the stack.

Why accessing data to the heap is more slow than allocating on the stack ?

Allocating space on the heap needs 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 find the way they use a computer’s memory while running.
Some languages have kept garbage collection that constantly looks for no longer used memory as the program runs.

The programmer must explicitly allocate and free the memory in other languages.
Rust uses a third approach wherein memory is kept through a system of ownership.
Ownership is managed with a set of defined rules that the compiler checks at compile time.
Ownership is the forwaring of currently possessed entity to another party which causes the previous owner to no longer have access to the object that is being transferred.
There are very clear rules about which piece of code owns a resource in Rust.
It’s the block of code that created the object representing the resource in the simplest case.
In the last of the block the object is destroyed and the resource is released.No one of the ownership features slow down our program while it’s running.
In simple words for understanding purpose……

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 finishes, and s is no longer valid

In this 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 it exist valid until the end of the parenthesis “}”.

The Rust’s unique feature is ownership which enables it to make memory safety guarantees without needing a garbage collector.

Why Ownership

Maintaining record 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 for that we 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 and kept on stack whereas for more complicated data types we have heap.
Ownership Rules
Rule # 1

Each value in Rust has a variable that’s known its owner.

Example
let a = “Hello world!”;

↙ ↘

Variable Value

In this example the variable i.e “a” is also the owner of the value “Hello world!”.

Rule # 2

There must only be one owner at a time

Example

let b = String::from(“Hello”); –> here variable “b” is the owner

let c = d; –> here the value of “c” is moved to variable “d” which now becomes the owner of “Hello”

Understanding both variables “a” and “b” are within the same scope.

Rule # 3

When the owner went out of scope, so does the value

Example

fn main() {

{                       
Enter fullscreen mode Exit fullscreen mode

–> “b” is not valid here, it’s not yet declared

    let a = “Hello";      
Enter fullscreen mode Exit fullscreen mode

–> “b” is valid from this point forward

            –> do stuff with “b”

}                 
Enter fullscreen mode Exit fullscreen mode

–> this scope is now over and “b” is no longer valid

}
For more details visit:https://www.technologiesinindustry4.com/2020/10/what-is-rust-ownership.html

Discussion (0)