Rust
December 2, 2026 6 min read

Ownership in Rust

HS
HEMANT SINGH
Technical Writer & Educator

1. What is Ownership?

Ownership is Rust's system for managing memory.

In many programming languages, memory is managed using a garbage collector or by manually allocating and freeing memory.

Rust uses a different approach called Ownership.

Every value in Rust has an owner. Rust uses ownership rules to determine when memory can be safely released.

Example

fn main() {
let name = String::from("Hemant");

println!("{}", name);
}

Here:

  1. name is the variable.
  2. name owns the String.
  3. When name goes out of scope, Rust automatically releases the memory owned by that string.

You do not need to manually free the memory.

2. Three Main Rules of Ownership

Rust follows three important ownership rules:

Rule 1: Each value has an owner.

Rule 2: There can only be one owner of a value at a time.

Rule 3: When the owner goes out of scope, the value is dropped (its resources are released).

These rules help Rust prevent many memory-related errors.

3. Understanding Scope

A scope is the region of a program where a variable is valid.

In Rust, a scope is generally defined using curly braces {}.

Example

fn main() {
let name = "Hemant";

println!("{}", name);
}

The variable name is available inside the main() function after its declaration.

Example: Variable Inside a Block

fn main() {
{
let number = 100;

println!("{}", number);
}

// println!("{}", number); // Error
}

The variable number is declared inside the inner block.

When that block ends, number goes out of scope and cannot be accessed outside it.

4. Stack and Heap Memory

To understand ownership, we should first understand two types of memory used by programs.

Stack

The stack stores data in a last-in, first-out (LIFO) manner.

It is commonly used for values whose size is known at compile time.

Examples include:

  1. Integer values
  2. Boolean values
  3. Floating-point values
  4. Character values

Heap

The heap is used for dynamically allocated data.

For example, a String can grow or shrink while the program runs, so its text data is stored on the heap.

Example

fn main() {
let age = 32;

let name = String::from("Hemant");
}

Here:

  1. age is an integer value.
  2. name is a String that owns its text data.
  3. The String value keeps track of information such as the text's pointer, length, and capacity.

Important: Ownership is not limited to heap memory. Rust applies ownership rules to values in general, including values stored on the stack.

5. Understanding Move in Rust

Move means transferring ownership from one variable to another.

When we assign a String to another variable, Rust moves ownership instead of automatically copying the heap data.

Example

fn main() {
let name1 = String::from("Hemant");

let name2 = name1;

println!("{}", name2);
}

Output:

Hemant

Here:

  1. name1 initially owns the string.
  2. name2 = name1 transfers ownership to name2.
  3. name2 becomes the new owner.

After this assignment, name1 can no longer be used.

Example: Using the Old Variable

fn main() {
let name1 = String::from("Hemant");

let name2 = name1;

println!("{}", name1); // Compilation error
}

This produces a compilation error because ownership has moved to name2.

Rust prevents both variables from independently owning the same string data.

6. Understanding Clone

Sometimes we want two independent copies of a value.

For this, Rust provides the .clone() method for types that implement the Clone trait.

Example

fn main() {
let name1 = String::from("Hemant");

let name2 = name1.clone();

println!("Name 1: {}", name1);
println!("Name 2: {}", name2);
}

Output:

Name 1: Hemant
Name 2: Hemant

Here:

  1. name1 owns the original string.
  2. name2 owns a separate cloned string.
  3. Both variables can be used independently.

Important: Cloning a String copies its string data. Cloning can involve additional memory allocation and copying, so use it when you actually need an independent copy.

7. Understanding Copy Trait

Some types are copied automatically instead of having their ownership moved.

These types implement the Copy trait.

Common examples include:

  1. i32
  2. u32
  3. f64
  4. bool
  5. char

Example

fn main() {
let number1 = 100;

let number2 = number1;

println!("Number 1: {}", number1);
println!("Number 2: {}", number2);
}

Output:

Number 1: 100
Number 2: 100

Here, number1 is still usable after assigning it to number2.

Why?

Because integer values such as i32 implement the Copy trait.

Copy vs Clone vs Move


Feature

Copy

Clone

Move

Meaning

Copies a value automatically

Creates a copy explicitly

Transfers ownership

Common example

let b = a; for i32

a.clone()

let b = a; for String

Original variable usable?

Yes

Yes

No, for the moved value

Typical use

Simple copyable values

Independent duplicate

Ownership transfer

8. Ownership with Functions

Ownership also applies when we pass values to functions.

When a non-Copy value such as String is passed by value, ownership moves into the function.

Example

fn display_name(name: String) {
println!("Name: {}", name);
}

fn main() {
let name = String::from("Hemant");

display_name(name);

// name is no longer usable here
}

Output:

Name: Hemant

Here:

  1. name initially owns the string.
  2. We pass name to display_name().
  3. Ownership moves into the function parameter.
  4. When the function finishes, its parameter goes out of scope.

The original variable cannot be used after the ownership transfer.

9. Returning Ownership from a Function

A function can return ownership to the calling code.

Example

fn create_name() -> String {
let name = String::from("Hemant");

name
}

fn main() {
let result = create_name();

println!("{}", result);
}

Output:

Hemant

Here:

  1. create_name() creates a String.
  2. The function returns the string.
  3. Ownership transfers to result.
  4. result becomes the owner.

Notice that name is returned without a semicolon because it is the final expression.

10. Returning Ownership to a Function

A function can also accept ownership and return it.

Example

fn give_back(name: String) -> String {
name
}

fn main() {
let name1 = String::from("Hemant");

let name2 = give_back(name1);

println!("{}", name2);
}

Output:

Hemant

Ownership moves in this sequence:

name1
|
v
give_back() parameter
|
v
name2

At each stage, there is one owner of the string.

11. Why Ownership is Important

Ownership helps Rust prevent common programming problems.

A. Preventing Use After Free

Using memory after it has been released can cause serious errors.

Rust's ownership rules help prevent this at compile time.

B. Preventing Double Free

If two variables independently tried to release the same heap allocation, it could cause memory corruption.

Rust's move rules prevent this problem for owned values.

C. Managing Memory Automatically

Rust releases owned resources when their owners go out of scope.

This happens without requiring a garbage collector.

12. Common Mistakes

Mistake 1: Using a String After Move

Incorrect:

fn main() {
let a = String::from("Rust");

let b = a;

println!("{}", a);
}

Reason: Ownership moved from a to b.

Mistake 2: Expecting String Assignment to Clone

Incorrect assumption:

let a = String::from("Hello");
let b = a;

This does not create two independent strings. It moves ownership.

To create an independent copy:

let a = String::from("Hello");
let b = a.clone();

Mistake 3: Confusing Copy and Clone

Copy happens automatically for suitable types.

Clone is an explicit operation.

For example:

let a = 10;
let b = a; // Copy

let x = String::from("Rust");
let y = x.clone(); // Clone

13. Quick Reference


Concept

Example

Create a String

String::from("Rust")

Move ownership

let b = a;

Clone a String

let b = a.clone();

Copy an integer

let b = a;

Function takes ownership

fn display(s: String) {}

Function returns ownership

fn create() -> String {}

End of scope

Owner goes out of scope and value is dropped