E0073Assignment to
Error
Assignment to self
ErrorA method cannot assign a new object to self.
Why
An object is shared: every variable that holds it refers to the same object. Assigning to self would only change what the method calls self, not the object the caller has. To change the object, assign to its mut fields.
Broken
class Point {
mut x: i64
fn reset(self) {
self = Point(x: 0)
}
}
fn main() {
let p = Point(x: 1)
p.reset()
print(p.x)
}
What sisu says
error[E0073]: cannot assign to `self` --> E0073.sisu:5:9 | 5 | self = Point(x: 0) | ^^^^ | = help: an object is shared: `self` can already write every `mut` field sisu: 1 error sisu: for more on a code, run `sisu explain <code>`: E0073
Fixed
class Point {
mut x: i64
fn reset(self) {
self.x = 0
}
}
fn main() {
let p = Point(x: 1)
p.reset()
print(p.x)
}
Sisu