Methods
V does not have classes. But one can define methods for types. A method is a function that has a special receiver argument, only a receiver of the specified type can execute this function. The receiver has its own argument list between fn
and the method name.
This code above realizes two methods for receivers u
of type User
. Note that the method has_birthday()
has a mut
receiver, this is needed here since we want to change its data. The convention of V is not to use receiver names like self
, this
or similar things but a short, preferably one letter long, name.
Exercises
Create a method for the type
Person
that determines whether a person is underage or not.Create a method that determines whether an
Animal
has fur or not.
Last updated