Static Methods
It’s great that we can move our price
and increase
functionality from standalone functions into the Fruit
type itself. Like I mentioned above, this means we’re not cluttering up everything with extra function names. However, we still have one annoying function just sitting around: new_fruit
. It would be repetitive to have a new_fruit
, new_book
, new_missiles
, etc. set of functions all over the place.
There’s another concept inside an impl
block which will help with this: static methods. A static method is a method that doesn’t take a self
as the first parameter. So, for example, we can change new_fruit
into a new
static method with the code: