Theme
Q001. What is ownership?
Short Answer
Ownership is Rust's compile-time model for assigning responsibility for values and resources. Moving a non-Copy value transfers that responsibility and makes the source unavailable. When the current owner reaches its drop point, Rust runs the value's cleanup. Together with borrowing, this gives memory safety without a tracing garbage collector.
Deep Dive
Ownership is primarily about lifecycle responsibility, not permission to access data. An owning value may itself own fields, a heap allocation, or a handle to an external resource. Its type defines what cleanup, if any, must happen when that value is dropped.
For Copy types, assignment creates an independent value, so both bindings remain usable. For non-Copy types, assignment normally moves the value. Rust then rejects later use of the moved-from place in safe code.
Ownership is broader than heap memory. It also models responsibility for file descriptors, sockets, lock guards, and other resources with deterministic cleanup.
Internal Model
Ownership is not represented by a universal runtime owner pointer or owner ID. The compiler tracks whether places are initialized, moved, or borrowed, then emits cleanup code only for values that remain initialized.
Conceptually, a String contains a pointer, length, and capacity, while its bytes live in a heap allocation. The exact field layout is not a stable API guarantee. Moving the String transfers responsibility for that allocation; it does not copy the string bytes.
Example
rust
fn main() {
let owner = String::from("rust");
let new_owner = owner;
println!("{new_owner}");
// println!("{owner}"); // error: owner was moved
}Common Mistakes
- Treating ownership as runtime reference counting.
- Thinking ownership applies only to heap allocations.
- Saying ownership alone defines reference validity; borrowing and lifetimes complete that part of the model.
Follow-ups
- Why did Rust choose ownership instead of mandatory garbage collection?
- What changes when a value implements
Copy? - How does the compiler represent moved and initialized places?
Related
- Q002. Why did Rust introduce ownership?
- Q006. What is the difference between an owner, a binding, a value, and a resource?
- Q008. How is ownership enforced and represented under the hood?
References
- The Rust Programming Language
- Rust Reference