9.6 The self
Parameter
When defining methods, you decide how self
is taken:
self
: Full ownership; the instance is moved.&self
: Immutable reference; data is not modified.&mut self
: Mutable reference; data can be modified.
Use self
if you need to consume the instance, &mut self
if you need to mutate it, and &self
otherwise. This explicitness helps ensure safe concurrency and memory management.