Numbers are small and have a fixed size, so Rust can copy them freely. Text is different. A topic title can be any length and can grow while the program runs, so String::from("Rust") stores its text in a separate area of memory that the String manages. When the program is done with that text, the memory must be released, exactly once. Release it twice and the program can corrupt memory; never release it and memory leaks away.
Many languages solve this with a garbage collector that periodically looks for unused memory. Rust uses a simpler rule that the compiler checks: every value has exactly one owner, a binding responsible for it. When the owner goes out of scope, at the closing brace where it was created, Rust releases the value automatically. Assigning a String to another binding moves ownership: the new binding becomes the owner, and the old one may no longer be used. There is still only one owner, so the text is still released exactly once.
Predict and run
An integer like i32 implements Copy, which means assigning it to a second name copies the value, and both names stay usable. Follow the type of the value when you predict what an assignment does. Which name can print the String? How many integer names can still be used?
fn main() {
let draft = String::from("Rust");
let final_title = draft;
let minutes = 25;
let copy = minutes;
println!("{final_title}: {minutes} and {copy}");
}Check your prediction
Rust: 25 and 25After let final_title = draft;, only final_title owns the text. Both integer names work, because copy received its own 25. Now add a line that prints draft after the assignment:
fn main() {
let draft = String::from("Rust");
let final_title = draft;
println!("{draft}");
}The compiler reports error[E0382]: borrow of moved value: draft. It marks three places: where the value was created, where it was moved (value moved here) and where you tried to use it again (value borrowed here after move). The word "borrow" here just means that printing needs to look at the text. Undo that line before you continue.
Passing a String to a function moves it
A function parameter is a binding too. Calling a function with a String argument moves the String into that parameter, and when the function ends, its parameter goes out of scope and the text is released. To keep using the text afterward, the function can hand ownership back by returning it.
fn announce(topic: String) -> String {
println!("Starting {topic}");
topic
}
fn main() {
let topic = String::from("Ownership");
let topic = announce(topic);
println!("Back in main: {topic}");
}Starting Ownership
Back in main: OwnershipThe String travels into announce and comes back as its return value, and main binds it again. If announce did not return it, a later println!("{topic}") in main would fail with the same E0382 error. The compiler would even add a note that the parameter takes ownership of the value and suggest borrowing instead. Passing values in and back out like this works, but it is clumsy; the next lesson shows how a function can look at a String without taking it at all.
clone makes a second owner
When you really need two independent Strings, ask for a copy with .clone(). It allocates new memory and copies the text, so each binding owns its own value.
fn main() {
let original = String::from("Rust");
let mut edited = original.clone();
edited.push_str(" basics");
println!("{original}");
println!("{edited}");
}Rust
Rust basicspush_str appends text to a String, which requires a mutable binding. Changing edited leaves original untouched, because they no longer share anything. Cloning has a cost proportional to the length of the text, which is why Rust never does it behind your back. Integers are cheap to copy, so they copy automatically; Strings are not, so you write .clone() when you mean it.
Three answers to a move error
When the compiler says a value was moved, ask what you meant. Did you mean to hand it over? Use the new owner's name. Did you only need to read it? Borrow it, as in the next lesson. Do you need two separate values? Call .clone().
Practice
Your turn
The starter does not compile: run it and read the E0382 message. It says where label was moved and where it was used afterward. Fix it without calling .clone(), so the program prints:
Starting Ownership
Ownership: 20fn announce(topic: String) {
println!("Starting {topic}");
}
fn main() {
let label = String::from("Ownership");
let minutes = 20;
announce(label);
let title = label;
println!("{title}: {minutes}");
}Hint 1: the idea
The call announce(label) moves the String into the parameter, and it is released when announce ends. For main to keep using the text, ownership has to come back out of the function.
Hint 2: a first step
Start with the signature of announce: which return type would let it hand the String back? Then decide which binding in main should receive it.
Worked solution
fn announce(topic: String) -> String {
println!("Starting {topic}");
topic
}
fn main() {
let label = String::from("Ownership");
let minutes = 20;
let label = announce(label);
let title = label;
println!("{title}: {minutes}");
}Starting Ownership
Ownership: 20Three things changed: the return type -> String, the tail expression topic that hands the value back, and let label = announce(label);, which makes label the owner again. After that, let title = label; is an ordinary move, and title owns the text when it is printed. minutes needed no change, because integers are copied rather than moved. Cloning label before the call would also compile, but it would copy the text just to print it once. The next lesson removes the round trip altogether: announce can borrow the String instead of taking it.
Completion is saved in this browser only. It does not measure mastery.