BorrowCheck › Rust language core

Ownership, move, Copy/Clone

Ownership as the exclusive responsibility to drop a value; move semantics; how Copy differs from Clone.

The topic explains ownership: every value has exactly one owner, and only the owner is responsible for dropping the value. The questions check what happens to a value on assignment, when it is passed to a function and returned from one: when it is moved, when it is copied, and how the Copy and Clone traits differ in meaning, not only in syntax. A separate part covers the places a value can and cannot be moved out of: a struct field, a Vec element, the contents of a Box, a value behind a &mut reference, as well as partial moves and reinitializing a variable. The last subtopic is about scope: when the compiler runs a destructor, in which order local variables and parameters are dropped, why shadowing does not drop a value while assignment drops the old one. The topic is the foundation for borrowing and lifetimes.

34 questions: 14 basic, 18 intermediate, 2 advanced. 6 info pages, 7 sources.

Subtopics

  1. Ownership as a property of a place
  2. Move semantics
  3. Copy vs Clone
  4. Ownership across function calls
  5. Partial moves
  6. Scope and drop

Previous: Syntax, variables, types · Next: Borrowing, references, NLL