Ownership

All variable bindings have a scope where they are valid and it is an error to use a variable outside its scope:

  1. struct Point(i32, i32);
  2. fn main() {
  3. {
  4. let p = Point(3, 4);
  5. println!("x: {}", p.0);
  6. }
  7. println!("y: {}", p.1);
  8. }
  • At the end of the scope, the variable is dropped and the data is freed.
  • A destructor can run here to free up resources.
  • We say that the variable owns the value.