You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Ownership is Rust's most distinctive feature and the key to its memory safety guarantees. Understanding ownership, moves, references, and borrowing is essential for writing Rust programs.
Rust enforces three ownership rules at compile time:
{
let s = String::from("hello"); // s owns the String
// s is valid here
} // s goes out of scope — the String is dropped (freed)
When you assign a heap-allocated value to another variable, ownership moves:
let s1 = String::from("hello");
let s2 = s1; // s1 is MOVED to s2
println!("{s1}"); // ERROR: s1 is no longer valid
println!("{s2}"); // OK: s2 owns the String
This prevents double free errors — only one variable can free the memory.
Types that are small and live on the stack implement the Copy trait. Assignment copies instead of moves:
let x = 5;
let y = x; // x is COPIED, not moved
println!("{x}"); // OK — integers implement Copy
println!("{y}"); // OK
| Copy types | Non-Copy types (move semantics) |
|---|---|
All integers (i32, u64, etc.) | String |
Floating-point (f32, f64) | Vec<T> |
bool | Box<T> |
char | Any type with heap data |
| Tuples of Copy types | Structs (unless they derive Copy) |
To explicitly duplicate heap data, use .clone():
let s1 = String::from("hello");
let s2 = s1.clone(); // Deep copy
println!("{s1}"); // OK — s1 is still valid
println!("{s2}"); // OK — s2 is an independent copy
Instead of transferring ownership, you can borrow a value using references:
&T)fn calculate_length(s: &String) -> usize {
s.len()
} // s goes out of scope, but it does not own the String — nothing is dropped
let s1 = String::from("hello");
let len = calculate_length(&s1); // borrow s1
println!("{s1} has length {len}"); // s1 is still valid
&mut T)fn add_world(s: &mut String) {
s.push_str(", world!");
}
let mut s = String::from("hello");
add_world(&mut s);
println!("{s}"); // "hello, world!"
Rust enforces two critical borrowing rules at compile time:
let mut s = String::from("hello");
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.