That was what I initially thought. However, if you have a setter that moves, you can use it like a setter that mutates. On the other hand, if you have a setter that mutates, I don’t think you can use it like a setter that moves. Correct me if I am wrong.
However, having setters that move seem to be slightly more counter-intuitive than setters that mutates, though.
Code example:
struct Foo {
val: int,
}
impl Foo {
fn inc(mut self) -> Foo { self.val += 1; self }
}
fn main() {
let mut x = Foo{val: 1,};
x = x.inc();
let val = x.val;
print!("{}" , val);
}
Playground link
I initially thought that this code would not compile, but the borrow checker is apparently OK with this. Also note that x does not have to be mut to be able to call x.inc(), since it is mut self, not mut& self. (In that case, it would be impossible to reassign the result to x, though.)