The guideline (which claims to be approved by RFC #344) says that the setter should have the format
set_foo(&self, val: T) (or
set_foo(&self, val: &T), but I’d ignore this issue for the rest of this post). However, a method with such a signature could not function as a setter at all.
So what is the right signature?
- Mutates the value:
set_foo(&mut self, val: T)(Modifies the value in-place)
- Moves the value:
set_foo(self, val: T) -> SomeType(Consumes the value, modifies it in place, and returns it)
- Clones the value:
set_foo(&self, val: T) -> SomeType(Do a functional update on a value and return the new value)