In this lecture, we are going to talk about one final concept before moving on to WebAssembly.
I know you're excited to get started.
I promise this is the last topic before working on a real project.
There's a special type in rust called results.
It's a very common type.
You'll likely encounter this type whenever working on a project.
The idea of the result type is simple.
The result type can store two types of values a valid value or an error.
Just because our code compiles doesn't mean we won't encounter errors.
For example, let's say we need to open a file.
What if the file fails to open?
Here's another scenario we may need to connect to a database.
It's possible the hosting provider may be down.
External forces beyond our control can cause errors.
Rust provides a type for storing errors or values.
Let's give it a try.
Above the main function, let's define a function called is verified.
This function will accept an argument called accounts annotated with the bank account type.
For extra precaution, we should borrow the value by adding the ampersand symbol at the beginning of
the tape.
As the name suggests, this function will check if an account is verified, if they are, we will return
a Boolean value on top of returning, i.e. Boolean value.
We should throw an error if the account is not verified.
There are two possible scenarios either the accounts can be successfully verified or it produces an
error for the return type of this function.
We will send it to results.
We can hover our mouse over this type to learn more information.
An error will appear, but we can ignore it.
Scrolling to the bottom, we're given a description that says the following result is a type that represents
either a success or failure.
The result type can act as a container for more data about the results.
The compiler requires us to annotate the values for successful or failed operations.
We can do so with a feature called generics.
You can think of generics as function arguments, but with types and set of values, they're commonly
used with functions and structures.
Generics can be added to a type by adding a pair of angle brackets inside these brackets.
We can specify the values contained in the result type.
The first type will correspond to a successful results.
The second type will correspond with the error for this demonstration.
We're going to set both types to Boolean types.
We can begin writing the logic for the function inside the function.
We're going to run a match on the account verified property.
Let's handle both scenarios.
The field can either be true or false.
If the field matches with true, we are going to return a function called OK.
We are obligated to return a value with the type of results the OK function will return this type for
us inside this function.
We can add the value for a successful results.
It must correspond to the first type in the generic earlier we specify the result type will contain
a Boolean if the operation was a success.
Let's pass in true.
What about errors?
Turns out there's a function for producing a value with the result type for errors.
Let's return a function called error.
Same as before in our generic, we specify the type for the era value will be a Boolean.
Let's pass in false.
We can begin handling the results, switching back to the main function.
We're going to store the results in a variable.
The name of the variable will be called verification status.
The value will be the is verified function with the my account variable.
Remember to add the ampersand symbol.
The function is expecting to borrow in accounts.
Lastly, let's print this variable with the print line macro.
The results can't be printed directly.
They must be formatted beforehand.
We're finished.
Let's try compiling the project.
In the output, we'll find the return value contained in the result type.
However, it's not entirely ideal in most cases, we're not interested in the result container.
You may want to directly grab the value.
Here's where things get interesting.
Rust offers various solutions for accessing a value from a successful result.
We're not going to be able to explore every option, but let's look at the most common approaches for
retrieving a value.
The first option is to chain a function called unwrap on the results.
The unwrap function is a function you will come across often it'll grab the value from a successful
result.
Let's try compiling the project again.
This time, U-value is retrieved.
What do you think would happen if the result contained an error?
Let's find out inside the my account variable.
I'm going to update the verified field to false.
Next, I'll compile the project.
By default, Russell will panic when an error is thrown by a function, the unwrap function will never
return a value.
As a result, we can never view the results.
The error produced by Russell should give you enough information as to what's going on.
There's an alternative function called the expect function.
Let's replace the unwrap function with the expected function.
The expect function performs a similar task to the unwrap function.
It'll retrieve the value from a successful results, however, it'll allow us to output a custom error
message if an error is given.
We can write the message inside the expect function.
Let's pass in the following message.
Unable to unwrap results.
Time to compile the project one more time.
Russ will output the same error message with our error message mixed in perfect.
We have two functions for grabbing the response from a result type.
Admittedly, this isn't the best example of how to use the result type.
We have complete control over when an error gets produced.
The purpose of the result type is to handle actions that can produce an error.
When we start working on the mini project, we'll come across real examples of how we can use the result
type.
There's one more thing I want to mention in the resource section of this lecture.
I provide a link to a chapter from the official Rust book error.
Handling is a big subject in the Rust community.
There are various opinions and approaches to handling errors.
It's not as simple as it may seem a good starting point for learning more as this chapter from this
book.
I highly recommend giving it a read.
After finishing this mini course in the next lecture, we are going to begin developing a real web assembly
project.
I hope you're ready.
It's going to be a great learning experience.
I'll see you there.