Logo

Developer learning path

Rust

What is Ownership? in Rust

What is Ownership?

22

#description

Ownership is a fundamental concept in Rust that refers to how the memory used by a program is managed. At its core, it involves determining which parts of the code have "control" or "ownership" over certain pieces of data, and therefore have the ability to modify or delete that data.

In Rust, there can only be one owner of a piece of data at any given time. When a new piece of data is created, the code that creates that data becomes its owner. At this point, the owner is responsible for managing the memory used by that data and ensuring that it is properly deallocated when it is no longer needed. The owner can also transfer ownership to another part of the code, which then becomes responsible for managing the data.

Along with ownership, Rust also has a concept called borrowing, which allows multiple parts of the code to access a piece of data without becoming its owner. This is useful when you need to share data between different parts of the code without giving any one part complete control over it.

By enforcing ownership and borrowing rules, Rust is able to prevent common memory-related issues like null pointer errors, dangling pointers, and memory leaks. It also allows Rust to provide guarantees around memory safety and thread safety, making it a great choice for writing reliable and performant systems code.

March 27, 2023

If you don't quite understand a paragraph in the lecture, just click on it and you can ask questions about it.

If you don't understand the whole question, click on the buttons below to get a new version of the explanation, practical examples, or to critique the question itself.