Rust
Beginner
1 min read
The Ownership Rules
Example
fn main() {
// --- Stack-allocated Copy type ---
let a: i32 = 5;
let b = a; // 'a' is COPIED; both a and b are valid
println!("a={a} b={b}");
// --- Heap-allocated String: MOVE semantics ---
let s1 = String::from("hello");
let s2 = s1; // s1 is MOVED into s2; s1 is no longer valid
// println!("{s1}"); // <-- compile error: value borrowed after move
println!("s2 = {s2}");
// --- Clone to make an explicit deep copy ---
let s3 = String::from("world");
let s4 = s3.clone();
println!("s3={s3} s4={s4}"); // both valid after clone
// --- Ownership and functions ---
let greeting = String::from("hi");
takes_ownership(greeting);
// println!("{greeting}"); // compile error: moved into function
let num = 42i32;
makes_copy(num);
println!("num still valid: {num}"); // ok — i32 is Copy
// --- Returning ownership ---
let returned = gives_ownership();
println!("returned = {returned}");
} // 's2', 's4', 'returned' are dropped here
fn takes_ownership(s: String) {
println!("took ownership of: {s}");
} // s is dropped here
fn makes_copy(n: i32) {
println!("copy of: {n}");
}
fn gives_ownership() -> String {
String::from("transferred")
}