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
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 validExplanation
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
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
Inside function: world
Inside function: 42
n is still usable: 42Key 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
1. What happens when a String is assigned from one variable to another in Rust?
2. Which of these types implements the Copy trait by default?
3. When does Rust drop a value that is no longer needed?
4. What is the compile-time error you get if you use a variable after its value has been moved?
5. Passing a Vec<i32> by value into a function does what to the caller's variable?
Was this page helpful?
You May Also Like
Borrowing and References in Rust
How Rust lets code access data without taking ownership, using immutable and mutable references checked by the borrow checker.
The String Type in Rust
Rust's owned, growable, heap-allocated String type versus the borrowed &str string slice, and how to build and combine them.
Lifetimes in Rust
How Rust's lifetime annotations describe how long references remain valid, preventing dangling references at compile time.
Structs in Rust
Structs let you group related values into a single named type with fields you access by name.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics