Skip to content

Q006. What is the difference between an owner, a binding, a value, and a resource?

Short Answer

A binding is a name introduced by a pattern. A value is typed data produced and manipulated by the program. The owner is the binding or containing value responsible for that value's lifecycle. A resource is something the value manages, such as an allocation or file handle. Moving a value changes its owning place without necessarily moving the underlying resource.

Deep Dive

These terms describe different layers:

  • document can be a binding naming a Document value;
  • the Document value owns its String field;
  • the String value owns a heap allocation containing UTF-8 bytes;
  • after a move, a new binding owns the Document, while the allocation remains where it was.

Not every binding owns its referent. A binding of type &T owns the reference value itself but only borrows the T. Values can also be owned by struct fields, enum variants, collections, and temporary expressions without a persistent variable name.

Internal Model

The compiler reasons about places: expressions that identify storage locations such as locals, fields, or dereferences. Ownership state is associated with whether those places are initialized and usable, not with a runtime owner object.

At runtime, the owning value may be held in stack memory, registers, or another allocation. Its resource may live elsewhere, and the optimizer may eliminate bindings or storage locations entirely.

Example

rust
struct Document {
    text: String,
}

fn main() {
    let document = Document {
        text: String::from("ownership"),
    };

    let archived = document;
    println!("{}", archived.text);
}

Common Mistakes

  • Treating a variable name, a value, and its resource as the same object.
  • Assuming only named local variables can own values.
  • Assuming a reference owns the value it points to.

Follow-ups

  • What is a place expression?
  • Can a temporary value own a resource?
  • What exactly changes when document is moved?

References

  • The Rust Programming Language
  • Rust Reference