Mutable receiver methods

Use mut self for a method that changes a struct value. A call such as counter.bump() borrows the receiver automatically and evaluates it once.

The receiver must permit mutation. You do not need a raw pointer or an ownership annotation to call the method.

struct Counter { value: int }

impl Counter {
  fn bump(mut self) { self.value += 1 }
}

fn main() {
  let mut counter = Counter { value: 1 }
  counter.bump()
}
Run locally
moon check main.mn
Ctrl/⌘+Enter checks the current lesson.