E0018
ErrorSafe fix
mut self on a method
ErrorSafe fixA method of a class or an enum takes self, never mut self.
Why
An object is shared, so self can already write every mut field of the class. A mut self would suggest that the method could replace the object, which no method can do. An enum value never changes, so mut self on an enum method has nothing to mean; the help is to return a new value. Removing the mut is an unsafe fix there, since the body may still assign self.
Broken
class Counter {
mut n: i64
fn bump(mut self) {
self.n += 1
}
}
fn main() {
let c = Counter(n: 0)
c.bump()
print(c.n)
}
What sisu says
error[E0018]: a class method takes `self`, not `mut self` --> E0018.sisu:4:13 | 4 | fn bump(mut self) { | ^^^ | = help: an object is shared: `self` can already write every `mut` field help: remove the `mut` | 4 - fn bump(mut self) { 4 + fn bump(self) { | sisu: 1 error sisu: for more on a code, run `sisu explain <code>`: E0018
Fixed
class Counter {
mut n: i64
fn bump(self) {
self.n += 1
}
}
fn main() {
let c = Counter(n: 0)
c.bump()
print(c.n)
}
Sisu