Ownership
Ownership is Rust's most important feature. It is what makes Rust different from other languages. It is what allows Rust to achieve memory safety without a garbage collector, therefore making it a fast and efficient language.
In fact most programmers that work with systems programming languages like C and C++ migrate to Rust because of this feature, because those other programming languages don't have a safe system like ownership to prevent the programmer from writing unsafe code.
What is a garbage collector?
Before getting into ownership, let's first see how other higher level programming languages like JavaScript, Python and Go manage memory.
In languages like JavaScript and Python, memory is managed by a garbage collector. The garbage collector running at runtime keeps track of memory that is no longer needed and it frees them up. This is a great feature because it helps developers focus on writing code without worrying about memory management.
The problem with garbage collection is that it adds some overhead to the runtime, which can slow down the program.
The goal with ownership is to have the same memory safety and management as a garbage collector, but without the overhead.
What is Ownership?
Understanding ownership is a crucial part of learning Rust, without understanding ownership you'll fall into common pitfalls that makes it difficult for you to even compile your code. Once you understand ownership, you'll be able to write safe and efficient code with ease, you'll also have a better understanding of the compiler's error messages and how to fix them.
Ownership is a set of rules that the Rust compiler checks at compile time. These rules are enforced by the compiler to ensure that memory is handled correctly and efficiently, if you break these rules, your code will not compile.
Since ownership is a new and unique concept in programming, it could be a bit difficult to grasp at first, but you will slowly begin to understand it and you'll see how it makes your code safer and more efficient.
Understanding ownership also sets a foundation for you to understand other concepts in Rust, after you understand ownership, you'll find it easier to understand the next lessons that we're going to cover.
Let's start exploring the rules of ownership in the next lesson.