Maybe one thing to add, in addition to methods, you can also have associated functions, by leaving out the &self
parameter:
impl Point {
fn new(x: i32, y: i32) -> Point {
Point { x, y }
}
}
You call this function like so: Point::new(3, 5)
The example is quite deliberate. Most of the time, these associated functions are used for constructing the type itself, especially if you need to do more complex things for that than just initialize the struct. At that point, you don't have an object yet, so you couldn't be taking it as &self
anyways. ๐