In this lecture, we are going to discover data types by diving in two functions, as we've learned
functions are declared with the F and key word rust automatically calls the main function, writing
our entire logic into one function may not be the most efficient way to write code.
Let's try defining additional functions.
Before we do, let's empty out the main function to start with a fresh slate.
Above the main function define a new function called AD.
As the name implies, we're going to define a function for adding to numbers.
Our function should return the results.
We can start by adding arguments to the function.
We're going to limit our function to two arguments.
They'll be called NUM one and two, respectively.
After adding these arguments, the compiler will throw an error.
Arguments must be explicitly typed, if you can recall.
We never had to add types to our variables.
Variables can be inferred.
Let's look at an example.
Inside the main function, let's define a variable called through with an initial value of false.
The data type of the food variable is a Boolean type.
We can hover our mouse over this variable to double check the data type.
Russ can guess the data type of our variable based on the value we initialized it with.
This feature is called type inference.
All variables must have a data type whenever possible.
Russ will help us with annotating a variable with a data type.
In some cases, variables may need to be given a type explicitly.
For example, let's say we don't initialize the variable with a false value by removing this value.
Russ can't guess the data type of our variable.
It'll throw an error.
Therefore, we should help the compiler by annotating the variable.
Adding each type to a variable is called a type annotation.
A type annotation can be added by adding a colon character after the variable name.
This character is followed by the name of the type.
In the resource section of this lecture, I provide a link to a list of types in rust.
The type we're looking for is called Boolean or BOOL for shorts.
Back in our editor, let's apply this type to our variable.
After adding the type, the compiler has been satisfied, hopefully you can probably guess why the ad
function is not working.
If we hover our mouse over the functions arguments the compiler is informing us of its inability to
infer the types.
This doesn't just apply to our function, but all functions function arguments must be annotated with
a type.
The goal of our function is to get the sum of two numbers.
Therefore, our functions arguments should be annotated with a data type suitable for numbers.
Believe it or not, Russ has about a dozen data types four numbers which may appear abnormal with so
many choices.
Which data type do we use?
The most common data type is ia32.
For example, let's update our few variable to a random number.
We can hover our mouse over the variable to inspect the data type by default, Ruston for is a data
type of I-30, too.
It's a standard type for numbers.
You can refer to the page I provided earlier for a complete list of data types for numbers, but you
may be asking Why are there so many data types for numbers?
Keep in mind, rust is a memory efficient language.
Not all numbers are equal.
Some numbers can range from one to 100.
Other numbers can range from one to one billion.
As you can imagine, the larger a number, the more memory it occupies.
We can save memory by being precise with our data types.
If your variables will store small numbers, you may benefit from using a data type suitable for small
numbers.
For example, the AI 16 data type can store small numbers if you need to store large numbers.
You can use the AI 64 data type by default.
Russ likes to use the i30 two data type to cover most cases.
I think it will be suitable for our ad function.
Let's annotate both arguments with the AI 32 data type.
We're not finished yet.
The return value should be annotated to this step is optional if we're not going to be returning a value.
However, our function will return the same.
Thus we should annotate the return type.
Return values can be annotated by adding an arrow after a function's argument list.
This arrow is followed by the data type.
We'll set the data type two I-30 to.
The last step is to add the arguments together inside the function at the following return NUM one plus
num to.
The return keyword can be used to return a value.
Interestingly, the return keyword is optional.
Russ will automatically return the value if the last line in the function is an expression.
We can all together remove the return keyword to get the same behavior.
The return key is required if you want to return the function earlier than planned.
Let's use our function.
Instead of assigning the Foo Variable to a number, let's set it to be value returned by the sum function.
We will pass in two numbers.
Behind the scenes, rust can infer a variables type from a functions return value, we can hover our
mouse over the few variable for further proof.
We don't need to annotate.
The few variable rust is capable of annotating the type with the functions.
Return type isn't type inference awesome.
It keeps our code readable.
It's time to output the number.
So far, we haven't compiled our project.
Nothing would happen if we were to compile it because we haven't told our program to output information.
Let's learn how to output information in the next lecture.