1
00:00:00,330 --> 00:00:06,390
In this lecture, we are going to discover data types by diving in two functions, as we've learned

2
00:00:06,570 --> 00:00:13,560
functions are declared with the F and key word rust automatically calls the main function, writing

3
00:00:13,560 --> 00:00:18,780
our entire logic into one function may not be the most efficient way to write code.

4
00:00:19,170 --> 00:00:21,810
Let's try defining additional functions.

5
00:00:22,020 --> 00:00:27,090
Before we do, let's empty out the main function to start with a fresh slate.

6
00:00:29,860 --> 00:00:34,210
Above the main function define a new function called AD.

7
00:00:36,890 --> 00:00:41,720
As the name implies, we're going to define a function for adding to numbers.

8
00:00:41,990 --> 00:00:44,210
Our function should return the results.

9
00:00:44,570 --> 00:00:47,330
We can start by adding arguments to the function.

10
00:00:47,630 --> 00:00:50,750
We're going to limit our function to two arguments.

11
00:00:51,020 --> 00:00:54,710
They'll be called NUM one and two, respectively.

12
00:00:57,480 --> 00:01:01,350
After adding these arguments, the compiler will throw an error.

13
00:01:01,740 --> 00:01:05,940
Arguments must be explicitly typed, if you can recall.

14
00:01:06,060 --> 00:01:08,850
We never had to add types to our variables.

15
00:01:09,180 --> 00:01:10,890
Variables can be inferred.

16
00:01:11,190 --> 00:01:12,880
Let's look at an example.

17
00:01:13,170 --> 00:01:19,770
Inside the main function, let's define a variable called through with an initial value of false.

18
00:01:22,350 --> 00:01:25,980
The data type of the food variable is a Boolean type.

19
00:01:26,280 --> 00:01:30,480
We can hover our mouse over this variable to double check the data type.

20
00:01:30,840 --> 00:01:36,540
Russ can guess the data type of our variable based on the value we initialized it with.

21
00:01:36,960 --> 00:01:39,510
This feature is called type inference.

22
00:01:39,870 --> 00:01:43,890
All variables must have a data type whenever possible.

23
00:01:44,040 --> 00:01:47,970
Russ will help us with annotating a variable with a data type.

24
00:01:48,660 --> 00:01:53,280
In some cases, variables may need to be given a type explicitly.

25
00:01:53,580 --> 00:02:00,630
For example, let's say we don't initialize the variable with a false value by removing this value.

26
00:02:00,720 --> 00:02:03,810
Russ can't guess the data type of our variable.

27
00:02:04,080 --> 00:02:05,260
It'll throw an error.

28
00:02:05,580 --> 00:02:09,600
Therefore, we should help the compiler by annotating the variable.

29
00:02:09,990 --> 00:02:13,650
Adding each type to a variable is called a type annotation.

30
00:02:14,310 --> 00:02:19,860
A type annotation can be added by adding a colon character after the variable name.

31
00:02:20,310 --> 00:02:23,130
This character is followed by the name of the type.

32
00:02:23,550 --> 00:02:29,160
In the resource section of this lecture, I provide a link to a list of types in rust.

33
00:02:31,630 --> 00:02:36,010
The type we're looking for is called Boolean or BOOL for shorts.

34
00:02:36,370 --> 00:02:40,150
Back in our editor, let's apply this type to our variable.

35
00:02:42,820 --> 00:02:49,930
After adding the type, the compiler has been satisfied, hopefully you can probably guess why the ad

36
00:02:49,930 --> 00:02:51,430
function is not working.

37
00:02:51,790 --> 00:02:58,300
If we hover our mouse over the functions arguments the compiler is informing us of its inability to

38
00:02:58,300 --> 00:02:59,500
infer the types.

39
00:02:59,860 --> 00:03:06,370
This doesn't just apply to our function, but all functions function arguments must be annotated with

40
00:03:06,370 --> 00:03:06,820
a type.

41
00:03:07,660 --> 00:03:11,110
The goal of our function is to get the sum of two numbers.

42
00:03:11,350 --> 00:03:17,260
Therefore, our functions arguments should be annotated with a data type suitable for numbers.

43
00:03:17,530 --> 00:03:24,490
Believe it or not, Russ has about a dozen data types four numbers which may appear abnormal with so

44
00:03:24,490 --> 00:03:25,390
many choices.

45
00:03:25,570 --> 00:03:27,520
Which data type do we use?

46
00:03:28,150 --> 00:03:31,060
The most common data type is ia32.

47
00:03:31,420 --> 00:03:35,820
For example, let's update our few variable to a random number.

48
00:03:38,470 --> 00:03:44,890
We can hover our mouse over the variable to inspect the data type by default, Ruston for is a data

49
00:03:44,890 --> 00:03:46,480
type of I-30, too.

50
00:03:46,840 --> 00:03:48,730
It's a standard type for numbers.

51
00:03:49,000 --> 00:03:55,540
You can refer to the page I provided earlier for a complete list of data types for numbers, but you

52
00:03:55,540 --> 00:03:59,110
may be asking Why are there so many data types for numbers?

53
00:03:59,590 --> 00:04:03,160
Keep in mind, rust is a memory efficient language.

54
00:04:03,400 --> 00:04:04,870
Not all numbers are equal.

55
00:04:05,110 --> 00:04:07,690
Some numbers can range from one to 100.

56
00:04:07,990 --> 00:04:10,720
Other numbers can range from one to one billion.

57
00:04:11,050 --> 00:04:15,940
As you can imagine, the larger a number, the more memory it occupies.

58
00:04:16,180 --> 00:04:19,779
We can save memory by being precise with our data types.

59
00:04:20,050 --> 00:04:26,560
If your variables will store small numbers, you may benefit from using a data type suitable for small

60
00:04:26,560 --> 00:04:27,160
numbers.

61
00:04:27,670 --> 00:04:34,480
For example, the AI 16 data type can store small numbers if you need to store large numbers.

62
00:04:34,600 --> 00:04:38,170
You can use the AI 64 data type by default.

63
00:04:38,380 --> 00:04:42,910
Russ likes to use the i30 two data type to cover most cases.

64
00:04:43,210 --> 00:04:45,880
I think it will be suitable for our ad function.

65
00:04:46,210 --> 00:04:50,170
Let's annotate both arguments with the AI 32 data type.

66
00:04:52,770 --> 00:04:53,940
We're not finished yet.

67
00:04:54,210 --> 00:05:00,840
The return value should be annotated to this step is optional if we're not going to be returning a value.

68
00:05:01,350 --> 00:05:04,020
However, our function will return the same.

69
00:05:04,320 --> 00:05:07,020
Thus we should annotate the return type.

70
00:05:07,350 --> 00:05:12,420
Return values can be annotated by adding an arrow after a function's argument list.

71
00:05:12,780 --> 00:05:15,090
This arrow is followed by the data type.

72
00:05:15,420 --> 00:05:18,030
We'll set the data type two I-30 to.

73
00:05:19,990 --> 00:05:27,760
The last step is to add the arguments together inside the function at the following return NUM one plus

74
00:05:27,790 --> 00:05:28,690
num to.

75
00:05:31,230 --> 00:05:34,860
The return keyword can be used to return a value.

76
00:05:35,100 --> 00:05:38,490
Interestingly, the return keyword is optional.

77
00:05:38,730 --> 00:05:44,160
Russ will automatically return the value if the last line in the function is an expression.

78
00:05:44,550 --> 00:05:48,870
We can all together remove the return keyword to get the same behavior.

79
00:05:49,230 --> 00:05:54,450
The return key is required if you want to return the function earlier than planned.

80
00:05:55,050 --> 00:05:56,460
Let's use our function.

81
00:05:56,760 --> 00:06:03,240
Instead of assigning the Foo Variable to a number, let's set it to be value returned by the sum function.

82
00:06:03,570 --> 00:06:05,550
We will pass in two numbers.

83
00:06:08,000 --> 00:06:15,020
Behind the scenes, rust can infer a variables type from a functions return value, we can hover our

84
00:06:15,020 --> 00:06:18,170
mouse over the few variable for further proof.

85
00:06:18,500 --> 00:06:20,030
We don't need to annotate.

86
00:06:20,030 --> 00:06:25,100
The few variable rust is capable of annotating the type with the functions.

87
00:06:25,100 --> 00:06:28,310
Return type isn't type inference awesome.

88
00:06:28,520 --> 00:06:30,200
It keeps our code readable.

89
00:06:30,800 --> 00:06:32,660
It's time to output the number.

90
00:06:32,960 --> 00:06:35,780
So far, we haven't compiled our project.

91
00:06:36,110 --> 00:06:41,840
Nothing would happen if we were to compile it because we haven't told our program to output information.

92
00:06:42,200 --> 00:06:45,830
Let's learn how to output information in the next lecture.

