Struct types
Structs in Rust have a few different ways to be defined, each having their own use cases.
Structs with named fields
The most common one is structs with named fields, here's an example of a struct with named fields:
Each field in the struct can have a different type. The fields can be accessed using the dot notation:
Tuple structs
Tuple structs do not have named fields, but rather just the types of the fields. They are especially useful when you know the struct type doesn't need too many fields, so there's no need to name them.
Here's an example of a tuple struct:
In the example above of a Color
struct, we don't need to name the fields, as it's clear that the three integers represent the red, green, and blue values of the color.
Tuple structs are accessed using the dot notation and the index of the field, the first field has index 0
, the second has index 1, and so on:
Unit structs
Unit structs are structs that don't have any fields. They are useful when you need to implement a trait on a type (more on traits and implementations later), but don't need to store any data. They are called unit structs because they have the type of ()
(unit type), as discussed earlier in the data types lesson.
If you only need to implement a trait on a type, you can use a unit struct:
Here's an example of a unit struct. Let's say we have a trait Describable
that defines a method describe
. Here's how you can define and implement this:
Don't worry if you don't quite understand traits and implementations yet, we'll cover them in detail in a later lesson.
Conclusion
There are three ways to define structs in Rust: structs with named fields, tuple structs, and unit structs.
Each has its own use cases, and you should choose the one that best fits your needs. Structs with named fields are the most common and are used when you need to name each field. Tuple structs are used when you don't need to name the fields, and unit structs are used when you don't need to store any data.
In the next lesson, we're going to explore implementations, what they are, how they work, and how to use them with structs.