Version: 1.1.0

Ownership

Argument conventions say whether a function borrows a value or takes it. The default is the safest one that still reads naturally.

Essentials

  • read borrows immutably. The caller keeps the value.
  • mut borrows and allows changes. Only one mutable borrow is active.
  • owned takes the value. The caller must transfer it.
  • var in a struct field means the struct owns that field.
  • Conventions are checked at compile time, so use-after-move is an error.

Example

fn show(read text: String):
    print(text)

fn rewrite(mut text: String):
    text += "!"

Keep in mind

Start with the defaults. Add mut or owned only when the function's job is to change or consume the value.