[No Audio]
In Rust programming, there are a couple of
enums that are very frequently used, and they
have a specific use in certain circumstances.
Their understanding is essential for becoming
proficient in Rust programming. These two
enums are the option and result enums. We will
cover the option enum in this tutorial, and
the result in them in the later on tutorial.
Let's start learning the option enum.
Sometimes it's, it's desirable to capture the
failure of some segment or part of a program.
This is typically accomplished using the
option enum in Rust. As discussed, each enum
type is associated with some variants. So the
first thing to note about the option enum is
that, it has two variants. The first variant
is called None, which is used to indicate
failure or lack of value. And the second
variant is called Some, which is basically a
tuple struct that wraps a value with type T,
where T is a generic type. Due to its
importance, the option enum are included in
the programs by default. Before using the
option enum, we'll look at how it is defined in Rust.
You may note that, it uses the
generic type T which can be any type, and
there are two variants None and Some, the None
variant is used to indicate a failure or lack
of a value, while the Some variant indicates
some value having any type. Let us look at a
simple example to see the basic use of the
option enum. We will first initialize a
variable of type Option which will have None
value. This syntax means that, the disease is
of type Option, which may have an associated
string value, in case of it Some variant. But
by default, it will have a null value which
means no disease by default. Next, we will
assume that for instance, a doctor has made
some investigations, and therefore he is able
to come to the conclusion that, the patient in
question has a disease of diabetes. So
therefore, the disease variable needs to be
updated with the proper disease value. So let
us update the disease variable from Some
variant and inside the Some, the name of the
disease in string format.
[No Audio]
This means now, that the disease has some
value, and is of Option enum type Some.
To check if the variant is Some or None for the
disease variable, we will use a match
statement, we will do a match on the disease.
[No Audio]
Next, we will include variants as arms of the
match. First, we will have an arm for the Some variant.
[No Audio]
Please note that, whatever variable
we mentioned inside the Some, it will be used
to capture the value of the disease. We do
not need to separately initialize a variable
of disease_name, it will be
automatically initialized from the value of
the disease that is wrapped by the Some
variant. Next I will add the arm for the None variant.
[No Audio]
Let us execute now.
[No Audio]
The print statement indicates, in this case, that the
disease is that of diabetes. One final note
before we move to the next example is, that of
the variable of disease_name is
limited in scope to the body of the match, and
will not be known outside the body of the
match. As pointed out in the previous
section, the variable scope is limited to the
curly brackets in which it lies. The option
enum can be used to check for any type of
data. Let us cover this using some suitable
examples. First, we will look at the string
slice type, I will declare a variable of
option type with string slice value.
[No Audio]
To extract the value out of the Some variant, we
will use the unwrap function, let us unwrap
the value of s1 in a print statement.
[No Audio]
Let us execute now.
[No Audio]
The first value is, the Some variant having
the value of string slice encapsulated or
wrapped inside it, and the second value is the
actual value of the string slice. This unwrap
function will be useful for obtaining the
value inside the Some variant. Let us too, have
an example on a numeric values also. Consider
a variable f1 having an option f64 value
initialized from some value of 10.54.
[No Audio]
We can perform operations on the value by unwrapping
the value. For instance, I want to
add it to some other variable, let's say f2
with a value of 16.5, then I will first unwrap
and then perform the addition.
[No Audio]
This will add the two values together. Please note that,
if I change the value of f1 to that of None
variant, then in that case, there will be an
error message, calling the unwrap on a None
value will cause the program to panic,
let us change back.
[No Audio]
Let us add a print statement and print the result of addition
to confirm that the values are being added together.
[No Audio]
Let us cargo run.
[No Audio]
Let us consider some other data types,
such as vectors, the syntax will be
exactly the same as we just learned. For
instance, I will declare an optional vector
of i32, and we'll initialize it from Some values.
[No Audio]
Let us finally learn the use of structures in
connection with the option enum, I will define
a simple Person struct.
[No Audio]
Let us have some instances of the structure.
[No Audio]
Next, I will create a variable of type
optional person, and we'll initialize it from
Some Person p1.
[No Audio]
Let us execute.
[No Audio]
In summary, the Option enum can be used with
almost any type of data and has two variants
of Some or None. As a final example, we will
see how to pass a variable of Option type as
a function parameter. I will define the
function called square, this function will
compute the square of the value or the number
that has been passed to it. The input and
output of the function are both variable
containing optional i32 values
[No Audio]
Next, inside the body of the function, I will
use the match statement to check if the
variable name is of type Some or None.
[No Audio]
First, I will include the arm for Some.
[No Audio]
In this case, I will return the square,
encapsulated inside the Some.
[No Audio]
Since the return is an Option,
therefore, we need to wrap the answer in some variant.
Next, in case of non variant, I will return a None.
[No Audio]
Let us call this from the main function now.
First I will define a variable having,
having Some value.
[No Audio]
Next, I only want to print out the square of
the value, when the call to the function
returns some value. I will include an if
statement for this, where I will check if the
square, if the square of the number
is not equal to None.
[No Audio]
In this case, I will print the square of the number.
[No Audio]
In case the function returns a None, so also we
will display an appropriate message.
[No Audio]
Let us execute now.
[No Audio]
Let us also call this function with an
a None variant and execute.
[No Audio]
Seems fine. Typically the Rust programmers
use the match with enums, but it is just a
convention and not a strict requirement, and
you are free to consider the use of if
statements also. That brings us to the end of
this tutorial, we have learned an important
type of enum called the Option enum, which
will be very frequently used in Rust
programming. The next tutorial is about the
second very useful enum called Result, which
is as important as the Option enum. Do come
back for covering that, and until next
tutorial, happy Rust programming.
[No Audio]