Ownership and moves in Rust
Learn Rust · Beginner · 10 min · After Variables, mut and shadowing in Rust
Who owns a value in Rust, and when is it freed?
1Guess
fn main() { let a = 5; let b = a; println!("{a} {b}"); }2Read
The ownership rules
Rust manages memory with three rules the compiler checks. Each value has an owner. There can only be one owner at a time. When the owner goes out of scope, the value is dropped. Dropped means Rust frees the memory for you, at the closing brace of the scope. You write no free call and there is no garbage collector.
3Code
A String and a function that returns it
1 / 2fn main() { let mut s = String::from("hello"); s.push_str(", world"); println!("{s}"); let t = take_and_give_back(s); println!("{t}"); } fn take_and_give_back(text: String) -> String { println!("got {text}"); text }Read the program. What will it print?
4Read
Move, Copy and clone
Assigning a
Stringto another variable moves it: the new variable owns the data and the old one can no longer be used. Types with a fixed size, such as integers, implement Copy and are copied instead. If you want a second, independentString, call.clone(). It copies the data, so use it when you need both.5Code
clone, then move into a function
1 / 2fn main() { let s1 = String::from("hello"); let s2 = s1.clone(); println!("{s1} and {s2}"); let len = calculate_length(s2); println!("length {len}"); } fn calculate_length(s: String) -> usize { s.len() }Read the program. What will it print?
6Fix
fn main() { let s1 = String::from("hello"); let s2 = s1; println!("{s1}, world!"); println!("{s2}"); }7Say it back
8Mission · 8 min
Write a program that makes a String, moves it into a function that prints it and returns its length, and prints the length. Then try using the String after the call and read the error.
cargo new owners cd owners cargo run
Common questions
- What is ownership in Rust?
- Ownership is the set of rules Rust uses to manage memory without a garbage collector. Each value has one owner, there can be only one owner at a time, and when the owner goes out of scope the value is dropped. The compiler checks these rules, so a program that breaks them does not compile.
- What does move mean in Rust?
- A move transfers ownership of a value. When you write let s2 = s1 with a String, s2 becomes the owner and s1 can no longer be used; the compiler reports an error if you try. Passing a String to a function moves it too. Simple types such as integers are copied instead of moved.
- When should I use clone in Rust?
- Call clone when you need a second, independent copy of a value such as a String, and both copies must stay usable. It copies the data, so it costs time and memory for large values. If you only need to read a value in a function, borrow it with a reference instead of cloning.
Sources
- https://doc.rust-lang.org/book/ch04-01-what-is-ownership.html (read 2026-12-03)
- https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html (read 2026-12-03)
Measured with rustc 1.100.1 (play.rust-lang.org, stable, edition 2024) on 2026-12-03
Published Dec 3, 2026 · Updated Dec 3, 2026