100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Rust

Ownership in Rust

Rust's core memory-safety system where every value has exactly one owner and is dropped when that owner goes out of scope.

Ownership & BorrowingBeginner9 min readJul 8, 2026
Analogies

Introduction

Ownership is the feature that sets Rust apart from almost every other systems language. Instead of relying on a garbage collector or forcing you to manually call free(), Rust enforces a small set of compile-time rules that guarantee memory safety with zero runtime overhead. Every value in Rust has a single variable that is its 'owner'. When that owner goes out of scope, Rust automatically drops the value and reclaims its memory - deterministically, and without a garbage collector.

🏏

Cricket analogy: Unlike a team manager who must remember to retire old kit (manual free()) or a groundstaff crew that periodically clears the shed (garbage collection), Rust assigns each piece of equipment exactly one owning player, and it's automatically returned to the store the moment that player leaves the field (scope).

Syntax

rust
let s1 = String::from("hello"); // s1 owns the String
let s2 = s1;                     // ownership MOVES to s2
// println!("{}", s1);           // compile error: s1 was moved

let x = 5;      // i32 implements Copy
let y = x;      // x is copied, not moved
println!("{} {}", x, y); // both still valid

Explanation

Three rules govern ownership: each value has exactly one owner at a time; when the owner goes out of scope, the value is dropped; and assigning a heap-allocated value like a String or Vec to another variable MOVES ownership rather than copying it. After a move, the original variable is considered invalid, and the compiler rejects any further use of it. Simple stack-only types (integers, bool, char, and tuples composed entirely of Copy types) implement the Copy trait, so assignment duplicates the value instead of moving it, and both variables remain usable.

🏏

Cricket analogy: A single bat belongs to one batsman at a time; handing it to a teammate moves ownership so the original batsman can no longer use it, but a simple item like a coin-toss result (Copy) can be freely duplicated to both captains without losing the original.

Example

rust
fn takes_ownership(s: String) {
    println!("Inside function: {}", s);
} // s goes out of scope here, and String's memory is dropped

fn main() {
    let s = String::from("world");
    takes_ownership(s); // ownership moves into the function
    // println!("{}", s); // compile error: s was moved

    let n = 42;
    makes_copy(n); // i32 is Copy, so n is still usable after this
    println!("n is still usable: {}", n);
}

fn makes_copy(x: i32) {
    println!("Inside function: {}", x);
}

Output

text
Inside function: world
Inside function: 42
n is still usable: 42

Key Takeaways

  • Every value has exactly one owner at any given time.
  • When the owner goes out of scope, Rust automatically drops the value (deterministic RAII, no garbage collector).
  • Assigning or passing a non-Copy value (String, Vec, etc.) moves ownership; the old variable becomes invalid.
  • Types implementing the Copy trait (integers, bool, char, etc.) are duplicated on assignment instead of moved.
  • Ownership rules are enforced entirely at compile time, so they add zero runtime cost.

Practice what you learned

Was this page helpful?

Topics covered

#Rust#RustProgrammingStudyNotes#Programming#OwnershipInRust#Ownership#Syntax#Explanation#Example#StudyNotes#SkillVeris