Rust Reference ยท Concepts
Ownership rules in Rust
Reference for Rust's ownership rules: one owner per value, moves on assignment and calls, Copy types, partial moves, drop order and how to give ownership back.
Ownership is the rule set that lets Rust manage memory without a garbage collector and without manual free. There are three rules. Every value has exactly one owner. There can be only one owner at a time. When the owner goes out of scope, the value is dropped, which runs its destructor and releases its resources.
Because the compiler knows at every point who owns what, it can insert the cleanup code for you, and it can reject programs that would use a value after it was freed or free it twice. Assigning or passing a non-Copy value moves it: the new place becomes the owner and the old name can no longer be used.
let a = String::from("hi"); let b = a; // ownership moves from a to b // println!("{a}"); // error[E0382]: borrow of moved value
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
The rules in detail
Moves
A move copies the value's bytes to the new location and marks the old binding as uninitialized. For a String that means copying the pointer, length and capacity (24 bytes), not the heap text, so a move is cheap regardless of size. The old binding is then off limits until you assign to it again (if it is mut).
Moves happen on let b = a;, when passing an argument by value, when returning a value, when storing it into a struct or collection, and when a closure captures it with move. Method calls that take self (like into_iter() or into_bytes()) move their receiver too.
Copy
Types that implement Copy are duplicated bit for bit instead of moved: integers, floats, bool, char, shared references, raw pointers and tuples or arrays made only of Copy types. See move versus copy.
Drop order
Local variables are dropped in reverse order of declaration at the end of their scope. The fields of a struct are dropped in declaration order after the struct's own Drop::drop runs. Elements of a Vec drop from first to last. Temporaries are dropped at the end of the statement that created them (with exceptions for let bindings that extend them).
A value that was moved out is not dropped at its old location; its new owner drops it. You can drop early with drop(value), which is simply a function that takes ownership and returns.
Usage notes
- Partial moves: you can move one field out of a struct, and the other fields stay usable. You cannot then use the struct as a whole, and you cannot move out of a type that implements
Drop. - Moves out of borrowed content are errors.
let s = vec[0];for aVec<String>is rejected; clone, use a reference, or usestd::mem::take,swaporreplaceto take a value out and leave a stand-in. - Loops: moving a value inside a loop body fails on the second iteration unless you reassign it or break right after.
- Conditional moves are tracked with drop flags at runtime, so a value moved on only one branch is still dropped exactly once.
- Shared ownership needs a smart pointer:
Rcin one thread orArcacross threads, often with interior mutability. - Leaking is safe:
mem::forgetand reference cycles skip destructors. Ownership guarantees no double free, not that every value is freed. - Ownership is a compile-time idea. There is no runtime cost for tracking it, other than drop flags for conditional moves.
Related
- Borrowing rules, Move versus copy, Lifetimes,
Drop,Clone - Tutorial: Ownership, Drop and RAII, Copy and Clone