A method of a struct that change the value of the struct itself must be prefixed with the mutating
keyword
struct Counter {
private var value = 0
mutating func next() {
value += 1
}
}
The mutating
methods are only available on struct values inside variables.
var counter = Counter()
counter.next()
On the other hand, mutating
methods are NOT available on struct values inside constants
let counter = Counter()
counter.next()
// error: cannot use mutating member on immutable value: 'counter' is a 'let' constant