Focus mode

Rust Fundamentals

Understanding Ownership

Ownership is a concept that we are not familiar with from other languages and that makes the Rust programming language powerful, since it allows memory management. So, it is very important to understand this concept in order to understand Rust.

It would not be wrong to say that many errors you will encounter while programming with Rust will be caused by ownership concept. Since it is a concept that we are not familiar with in other languages, it is possible to have errors even in very simple code scripts. For this reason, we recommend that you watch these videos again and again when you have an error.

Ownership

The memory for a program can be allocated in a stack or a heap.

  • A stack follows a last in first out order and stores data values for which the size is known at compile time. Because their size is fixed, all scalar types can be stored on a stack.
  • Heap memory stores data values whose size is unknown at compile time, which may change throughout the program's lifecycle.

Ownership is a set of principles that determine how memory is managed in a Rust program and makes Rust unique.

Most programming languages either utilize a garbage collector or require the programmer to allocate and free memory. 

In low-level languages that do not have garbage collection; the software developer is responsible for the release of the variables and the resources they store. Generally, when processes with that resource are finished and it is no longer needed, the resource is freed. This operation is usually provided with structures such as pointers. When we're done with the pointer, we need to free the resource with the free function, if we don't it will lead to a memory leak. In other words, the program does not return this resource to the operating system until the computer is restarted, and this resource continues to occupy memory. 

In high-level languages have garbage collection. We don't have to think about memory management since garbage collection is responsible for performing these operations.

In Rust, each variable is responsible for freeing its own resources. A resource can have only one owner. When we assign another variable or give a variable as a parameter to the function, the owner of that resource is also transferred. When we transferred a resource, the previous owner of that resource is no longer available. This prevents a pointer from still pointing to a deleted resource.

Per Rust official documentation there are 3 Ownership rules:

  • Each value in Rust has an owner.
  • There can only be one owner at a time.
  • When the owner goes out of scope, the value will be dropped.

Rust's Ownership feature provides memory safety which is one of the benefits of using Rust. Ownership is directly related to the concept of move, borrow,references, and lifetimes. Move and borrow we will now cover references and lifetimes in later chapters. Understanding these concepts is very important in terms of using rust functionally. 

To understand ownership in Rust better, you can watch this video that explain the subject in detail here:

https://www.youtube.com/watch?v=2IxQgXQl_Ws 

Move

For most types in Rust, operations such as assigning a value to a variable, passing a variable to a function, or returning it do not copy the value but move it. Ownership of the source value is transferred to the target; the old variable becomes unusable.

To understand move in Rust better, you can watch this video that explain the subject in detail here:

 https://www.youtube.com/watch?v=2H-O43Hl94c

You can find detailed further information about Move from Rust Official documentation:

https://doc.rust-lang.org/std/keyword.move.html 

Borrow

When a heap variable is assigned to another variable, ownership of the assigned variable is transferred, and the first variable no longer has access to that resource. In some cases, it may be desirable to access the variable's data without transferring ownership of the variable. Instead of sending an object as a value using Rust's borrowing feature, we can send it as a reference.

The compiler statically checks that the reference object refers to a valid object and ensures that an object is not deleted as long as the reference exists.

You can find detailed further information about Ownership, References and Borrowing from Rust Official documentation:

https://doc.rust-lang.org/book/ch04-00-understanding-ownership.html 

Resources:

https://www.tutorialspoint.com/rust/rust_ownership.htm 

https://canaltinova.com/blog/posts/rustta-ownership-kavrami/

https://blog.logrocket.com/understanding-ownership-in-rust/

left-disk

Programs to Accelerate Your Progress in a Software Career

Join our 4-8 month intensive Patika+ bootcamps, start with the fundamentals and gain comprehensive knowledge to kickstart your software career!

right-cube

Test

Comments

You need to enroll in the course to be able to comment!