Moves and ownership
Ownership is the idea that makes Rust different, and the reason most people are here. A value has exactly one owner; passing it around moves it; and the variable you left behind is gone, not empty.
Every one of these rules produces a compiler error with a code attached, which is why this is the most-searched part of the language.
Articles
Section titled “Articles”- What is ownership in Rust?the one rule that explains the other rules
- Why does Rust have ownership?the two bugs it removes, without a GC
- Why does Rust move values instead of copying them?the assignment that transfers ownership, and why it has to
- What happens to the original variable after a move?it is not cleared, it is invalidated
- How to tell whether a type will move or copythe Copy test, and how to check it yourself
- How move semantics work in Rustwhat actually happens to the bytes on a move
- Why can’t you use a value after moving it?double free, prevented at compile time
- Why does my String not implement Copy?a heap buffer cannot be duplicated by memcpy
- How to pass ownership of a value to a functionand how to tell that you just did
- How to return ownership from a functionhanding a value back instead of cloning it
- When should you clone?three cases where clone is the right answer
- How to pass a Vec to a function without cloning ittake &[T] and stop the copies
- How to loop over a Vec without moving itthe & that keeps the Vec usable after the loop
- What is a partial move?moving one field, and why the struct is then poisoned
- How ownership works with structsfields move independently of the struct
- What happens when you assign one struct to another variable?whole-struct moves, and when they do not
- How ownership works with match expressionswhich arms move and which borrow
- How to fix ownership errors with a Vecborrow the element, or take it out
- How does Rust manage memory without a garbage collector?deterministic drop, and the cost of Rc
- How to understand ownership if you come from Python or JavaScriptmapping your intuition onto moves