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()
}
Examples use the moon toolchain version shown beside each result.
Run locally
moon check main.mn
Ctrl/⌘+Enter checks the current lesson.