Ownership, moves and cleanup
Ownership, moves and cleanup
A student name stored in String owns a growable text buffer. Ownership tells Rust who is responsible for that value. Moving the String transfers that responsibility; it does not make a second independently owned text buffer. This explains many compiler errors that initially feel surprising.
What you’ll learn
- Identify the owner before and after a String move.
- Choose borrowing or cloning when the original value is still needed.
Ownership, moves and cleanup in action
original owns Diya
Original first owns the String.
original owns DiyaMove ownership to registered
The assignment transfers ownership to registered without cloning the text buffer.
Move ownership to registeredregistered drops at scope end
Printing borrows registered for formatting.
registered drops at scope end
Who owns the value?
A String has a small representation containing information about its allocated text. Assigning it to another binding normally moves the value. The previous binding can no longer be used. When the owner goes out of scope, Rust drops the value and releases its owned resources. Shared ownership wrappers are deliberate alternatives, not exceptions you get from ordinary assignment.
Connect this idea with copy values and deliberate cloning and references and read-only borrowing.
Work through the Rust program
Save the complete source as main.rs in the Rust environment. No standard input is needed.
Rust 2024 · complete program
fn main() {
let original = String::from("Diya");
let registered = original;
println!("Registered: {registered}");
}Expected output
Registered: DiyaFollow the execution
Original first owns the String. The assignment transfers ownership to registered without cloning the text buffer. Printing borrows registered for formatting. At the end of main, registered is dropped; original is no longer a usable owner.
Try the complete program in the Rust compiler. For tests, Cargo commands and local files, use the specified local environment.
What moves through a function call?
A parameter declared as name: String takes ownership when called with an owned String. Unless the function returns that value, the caller cannot use the original binding afterward. A parameter name: &str instead borrows readable text. Choose the signature based on whether the operation must retain ownership or only inspect a value.
Stack and heap describe storage strategies. A String’s small representation is often held in a local variable while its growable text is allocated separately. A move transfers ownership; it is not a promise that the compiler physically relocates all text bytes.
Try a small variation
Your task: Can you print original after assigning it to registered?
Rust 2024 · complete program
fn main() {
let original = String::from("Diya");
let registered = original;
println!("Registered: {registered}");
}Worked answer
No. The String was moved, so using original afterward fails to compile. Borrow when another operation only needs to read it, or clone deliberately when an independent value is needed.
Mistakes and limits to recognize
The school-register analogy describes responsibility, not a real person physically relocating. Copy types behave differently, and ownership is not identical to immutability. Borrowing permits temporary access without transferring responsibility.
Check your understanding
Apply the rule
Take the idea into your next program
No. The String was moved, so using original afterward fails to compile. Borrow when another operation only needs to read it, or clone deliberately when an independent value is needed. The school-register analogy describes responsibility, not a real person physically relocating.
Further reading: the official Rust Book on ownership, moves and cleanup.
Reaching the recap and completing the quizzes are separate milestones.