Version: 1.1.0

Intro to value ownership

Every Mojo value has one place that is responsible for ending its life. That rule is what makes destruction automatic and predictable.

Essentials

  • Assigning a value usually transfers ownership of the unique resource.
  • An explicit copy is how you keep both the original and a duplicate.
  • Small types that are cheap to duplicate are marked trivial and copy freely.
  • The compiler tracks the last use and destroys the value there.
  • A reference lets another function read or modify a value without taking ownership.

Example

def consume(owned text: String):
    print(text)

def main():
    var name = String("Mojo")
    consume(name^)

Keep in mind

The ^ transfer operator makes a hand-off explicit. After the transfer, the original name cannot be used.