Rust – Concept of Ownership
Last Updated :
06 Oct, 2022
Rust programming language is known for its memory management. This article explains how Rust uses its Ownership model for better memory management. If you want to have a recap of Rust syntax before getting on this article you can refer to the Rust Archives.
Generally, there are 2 ways to manage memory in a program by using either Garbage Collects (or) programmer has to explicitly define the memory usage. Both the options have their advantages and disadvantages like explicit managing being faster but Garbage collection being easier to implement. Rust follows a new approach called “Ownership” which promises to be faster and will be easier when you get used to it. The ownership model is new for programmers but when you understand and start practicing you can get better at using it.
What is Memory Safety?
Memory safety is the property of a program where memory pointers always used to point to valid memory of the correct type and size. A memory unsafe program may crash or produce unexpected output and errors. It also may cause data leaks which in turn reducing the security of the program. One of the examples that can cause memory safety issues is Dangling pointers(pointers that point to invalid data (or) null).
Stack and Heap
Rust stores primitive datatypes directly into the memory but complex datatypes are generally stored in either Stack (or) Heap.
Stack stores variables of size at compile time. A stack follows a last in first out order. All scalar types can be stored in the stack as the size is fixed. If the size of the variable is unknown at compile time then they are stored in a heap which is suitable for dynamic datatypes. Heap is allocated used to store variables that may change throughout the life cycle of the program. Performing operations on a stack are faster than heap because of fixed size and direct access.
Rust Ownership Rules
Rust ownership rules are the guidelines (or) standards for the Ownership model. These are
- Each value in Rust has a variable called its owner.
- There can only be one owner at a time.
- When the owner goes out of scope, the value will be dropped.
Here is a simple Rust program following the above rules
Rust
fn main() {
print_gfg()
}
fn print_gfg(){
let gfg = "GeeksForGeeks";
println!("{}",gfg);
}
|
Output:
GeekdForGeeks
Let us look into the program once. The main function has only one line calling print_gfg() function. The print_gfg() function prints out the string “GeeksForGeeks”. We used the variable “gfg” to store the string “GeeksForGeeks”, so gfg will become the string, and as it is declared in print_gfg() function the scope of the variable where it is the owner and valid is till the end of the function.

Rust ownership
Multiple Variables and Data
In the above program, we have seen how a variable can access data, Now we will see an example of how multiple interacts with the same data.
Rust
fn main() {
print_gfg()
}
fn print_gfg(){
let gfg = "Geeks";
let gfg1 = gfg;
println!("{} For {}",gfg, gfg1);
}
|
In this Program, the variable gfg1 copies data in variable gfg but is valid because size of the variable is known before compile time, so Rust stores the variable in stack.

program valid
Now lets us look at a program where data is stored in heap and try to copy that data to another variable
Rust
fn main() {
print_gfg()
}
fn print_gfg(){
let gfg = String::from("Geeks");
let gfg1 = gfg;
println!("{} For {}",gfg, gfg1);
}
|
This gives an error because the size of gfg variable is unknown at compile-time and when we refer gfg1 to gfg the pointer moves from gfg to gfg1 rather than copying. To make it copy the data rather than moving it when need to use the clone method
Rust
fn main() {
print_gfg()
}
fn print_gfg(){
let gfg = String::from("Geeks");
let gfg1 = gfg.clone();
println!("{} For {}",gfg, gfg1);
}
|

clone
You can learn more about how Rust handles multiple data variables from References and Borrowing
Similar Reads
Rust - Concept of Ownership
Rust programming language is known for its memory management. This article explains how Rust uses its Ownership model for better memory management. If you want to have a recap of Rust syntax before getting on this article you can refer to the Rust Archives. Generally, there are 2 ways to manage memo
4 min read
Rust - Concept of Smart Pointers
Pointers are basically variables that store the address of another variable. While smart pointers are data types that behave like a traditional pointer with additional capabilities such as automatic memory management or bounds checking. What makes smart pointers different from normal pointers are:-
4 min read
Rust - Concept of Structures
Structures are basically user-defined type mainly used for grouping different data types together. It overcomes the limitation of a traditional array that can only contain elements of the same data type. A structure can also contain a structure in itself. In Rust, structures are similar to tuples, b
3 min read
Rust - Conventions
In Rust, there are certain conventions that need to be followed when we are creating dependencies. There are dependencies on libraries in Rust. Cargo! is a dependency manager in Rust that is similar to npm (in JavaScript) and pip (in Python). All the project dependencies are handled by cargo. For in
2 min read
Rust - Generic Function
In Rust, Generic functions are very useful. Generic make code more flexible and provide more functionality to the callers of the function. It prevents code duplication as it is not required to define different functions of different types. Generics are specified in the signature of function where we
3 min read
Rust - Concept of Data Freezing
Rust is a systems programming language. In Rust, there is a concept of freezing which states that whenever data is borrowed, the borrowed data is also frozen. This frozen data cannot be modified until the references get out of scope i.e. all the bindings go out of scope. Suppose in the given below e
1 min read
Conditionals in Rust
Conditional Statements are the decision-making statements based on given conditions. Conditional statements are common in programming languages and rust has them and unlike many of them, the boolean condition doesn't need to be surrounded by parentheses but is better to use, and each condition is fo
3 min read
Functions in Rust
Functions are the block of reusable code that can perform similar related actions. You have already seen one of the most important functions in the language: the main function, which is the entry point of many programs. Youâve also seen the "fn" keyword, which allows you to declare new functions. Ru
4 min read
How to Change Ownership Root to Apache?
Apache, formally known as the Apache HTTP Server, is one of the most widely used web server software systems in the world. Changing ownership of files from root to Apache is often necessary for web servers like Apache to have the correct permissions to read, write, and execute files. This is essenti
3 min read
Rust lifetime Constructor
In Rust, we have a concept of a lifetime constructor that falls under the ownership category. Rust enforces a certain set of rules through its concept of ownership and lifetimes. In this article, we would be learning more about Lifetimes in Rust. In Rust, there are various regions that are present i
3 min read