1
00:00:00,270 --> 00:00:04,980
In this lecture, we are going to learn how to create custom types with structures.

2
00:00:05,250 --> 00:00:07,680
We are going to be starting on a fresh project.

3
00:00:07,920 --> 00:00:10,800
I think the previous project was starting to get cluttered.

4
00:00:11,160 --> 00:00:17,330
I recommend moving into an empty directory when you're in an empty directory, initialize a new project

5
00:00:17,340 --> 00:00:19,560
by running the cargo net command.

6
00:00:22,040 --> 00:00:25,130
Next, let's open the main Port Arthur file.

7
00:00:27,590 --> 00:00:31,490
A structure can be thought of as a user defined type for grouping data.

8
00:00:31,730 --> 00:00:33,650
It's common for data to be grouped.

9
00:00:33,860 --> 00:00:37,160
For example, let's say we were creating a banking app.

10
00:00:37,430 --> 00:00:44,210
An account will hold various types of data, such as the owner's name, balance, interest rate, cetera.

11
00:00:44,750 --> 00:00:47,960
It's completely possible to avoid structures altogether.

12
00:00:48,170 --> 00:00:52,190
Theoretically, we can define variables for each of these values.

13
00:00:52,880 --> 00:00:59,540
However, if we need to create a new accounts, we will need to remember the values in an account by

14
00:00:59,540 --> 00:01:00,820
creating a structure.

15
00:01:00,890 --> 00:01:05,420
We can easily make sure every account has the same set of values.

16
00:01:05,810 --> 00:01:08,930
Rust enforces a strict model on structures.

17
00:01:09,560 --> 00:01:14,660
Let's take a closer look at structures by creating one at the top of the file.

18
00:01:14,690 --> 00:01:18,260
A structure can be defined by adding these struct keyword.

19
00:01:20,820 --> 00:01:23,490
Next, we need to give our structure a name.

20
00:01:23,760 --> 00:01:26,610
Let's give our structure the name of bank accounts.

21
00:01:29,210 --> 00:01:31,550
It's time to add fields into the mix.

22
00:01:31,760 --> 00:01:34,910
A field represents a single value in a structure.

23
00:01:35,210 --> 00:01:38,210
We can describe the makeup of structures with fields.

24
00:01:38,480 --> 00:01:41,210
The fields are in clothes with curly braces.

25
00:01:41,750 --> 00:01:46,130
The names for fields follow the same rules and conventions as variables.

26
00:01:46,430 --> 00:01:50,570
In addition, we must explicitly set the types of our fields.

27
00:01:50,960 --> 00:01:55,970
For this example, we will add two fields called balance and verified.

28
00:01:56,330 --> 00:01:58,970
The balance field will have an integer type.

29
00:01:59,330 --> 00:02:02,960
As for the verified field, it'll have a Boolean type.

30
00:02:05,630 --> 00:02:07,010
Our structure is ready.

31
00:02:07,220 --> 00:02:10,400
We have to find the data type, but we're not using it.

32
00:02:10,669 --> 00:02:16,430
If we want to create values with this structure, we can do so in a function inside.

33
00:02:16,430 --> 00:02:21,860
The main function will replace the existing code with a variable called my accounts.

34
00:02:24,580 --> 00:02:28,390
The value for this variable will be the bank account structure.

35
00:02:30,910 --> 00:02:36,700
Inside the curly braces, we must add the same fields defined in the bank account structure.

36
00:02:37,060 --> 00:02:40,000
This feature is one of the benefits of using a structure.

37
00:02:40,330 --> 00:02:44,980
We can hover our mouse over the structure to view the errors by the compiler.

38
00:02:45,400 --> 00:02:49,420
Our compiler is telling us we are missing two fields from the structure.

39
00:02:49,750 --> 00:02:54,220
It acts as a reminder whenever we're creating a new value with a structure.

40
00:02:54,550 --> 00:02:58,810
Let's add the balance and verified fields with random values.

41
00:03:04,390 --> 00:03:07,540
After adding these fields, the air should go away.

42
00:03:07,840 --> 00:03:10,660
Let's print the fields with the print line macro.

43
00:03:10,930 --> 00:03:14,050
We can access a specific field with dot notation.

44
00:03:14,440 --> 00:03:20,140
For example, let's try printing the balance field inside the print line macro.

45
00:03:20,170 --> 00:03:22,870
The string will contain a debug placeholder.

46
00:03:25,400 --> 00:03:30,230
For the second argument, we will pass in the my account balance field.

47
00:03:32,730 --> 00:03:35,250
Let's do the same for the verified field.

48
00:03:39,130 --> 00:03:43,330
Lastly, let's compile our project with the cargo run command.

49
00:03:45,740 --> 00:03:52,310
As you can see, we're able to access a field's value with dot notation, structures are very powerful

50
00:03:52,310 --> 00:03:54,380
because they allow us to group data.

51
00:03:54,740 --> 00:03:57,740
In addition, we benefit from its strictness.

52
00:03:58,010 --> 00:04:04,250
If we ever update a structure, our compiler will inform us of variables that aren't compliant with

53
00:04:04,250 --> 00:04:05,000
the structure.

