In this lecture, we are going to learn how to create custom types with structures.
We are going to be starting on a fresh project.
I think the previous project was starting to get cluttered.
I recommend moving into an empty directory when you're in an empty directory, initialize a new project
by running the cargo net command.
Next, let's open the main Port Arthur file.
A structure can be thought of as a user defined type for grouping data.
It's common for data to be grouped.
For example, let's say we were creating a banking app.
An account will hold various types of data, such as the owner's name, balance, interest rate, cetera.
It's completely possible to avoid structures altogether.
Theoretically, we can define variables for each of these values.
However, if we need to create a new accounts, we will need to remember the values in an account by
creating a structure.
We can easily make sure every account has the same set of values.
Rust enforces a strict model on structures.
Let's take a closer look at structures by creating one at the top of the file.
A structure can be defined by adding these struct keyword.
Next, we need to give our structure a name.
Let's give our structure the name of bank accounts.
It's time to add fields into the mix.
A field represents a single value in a structure.
We can describe the makeup of structures with fields.
The fields are in clothes with curly braces.
The names for fields follow the same rules and conventions as variables.
In addition, we must explicitly set the types of our fields.
For this example, we will add two fields called balance and verified.
The balance field will have an integer type.
As for the verified field, it'll have a Boolean type.
Our structure is ready.
We have to find the data type, but we're not using it.
If we want to create values with this structure, we can do so in a function inside.
The main function will replace the existing code with a variable called my accounts.
The value for this variable will be the bank account structure.
Inside the curly braces, we must add the same fields defined in the bank account structure.
This feature is one of the benefits of using a structure.
We can hover our mouse over the structure to view the errors by the compiler.
Our compiler is telling us we are missing two fields from the structure.
It acts as a reminder whenever we're creating a new value with a structure.
Let's add the balance and verified fields with random values.
After adding these fields, the air should go away.
Let's print the fields with the print line macro.
We can access a specific field with dot notation.
For example, let's try printing the balance field inside the print line macro.
The string will contain a debug placeholder.
For the second argument, we will pass in the my account balance field.
Let's do the same for the verified field.
Lastly, let's compile our project with the cargo run command.
As you can see, we're able to access a field's value with dot notation, structures are very powerful
because they allow us to group data.
In addition, we benefit from its strictness.
If we ever update a structure, our compiler will inform us of variables that aren't compliant with
the structure.