Impl blocks
Methods group into impl blocks:
impl Person {
fn greet(&self) {}
fn is_adult(&self) -> bool {
self.age >= 18
}
}
You can have multiple impl blocks for one struct. Fields are accessed with self.field inside methods.
The self reference
Methods receive self in three forms:
fn method(&self) {} // borrow
fn method(&mut self) {} // mutable borrow
fn method(self) {} // consume
&self is the most common. It lets the method read the instance without taking ownership.