Structs are like structs/classes in other languages. Structs have fields. We can instantiate structs.
Only mutable instances may be mutated:
A bit like in JS, we can use the shorter way to instantiate objects:
Also, we can create an instances based on other instances:
Tuples may be defined with a name, like this:
The fields don’t have names, sometimes it’s ok for simple types.
Structs may be completely empty:
It’s useful with traits.
Methods are defined outside of struct definition:
The first paramtere of a method is
&self. It’s a sugar syntax for
self: &Self. We could also not use reference, or add
mut. The first case is rare
because it would take ownership. It could be useful if a method is supposed to
transform an object info something else end the original object would not be
We can invoke methods like this:
Methods may be defined as non-associated if they do not require an actual
instance of a type. In such a case, there is no need for the first parameter to
Self. Example of such a function is
String::from(). It’s also useful for