This is the 29th day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021

option<T>

This article focuses on Option, which is another enumeration defined by the standard library. Option is more widely used because it sets up a very common scenario where there is either a value or no value.

Expressing this concept in terms of a type system means that the compiler needs to check at compile time that it handles all the cases it should handle, thus avoiding bugs that are common in other programming languages.

Rust does not have the null-value functionality found in many other languages. A Null value, “Null,” is a value that represents no value. In a null-valued language, a variable is always in one of two states: null and non-null.

The problem with null values is that when you try to use a null value like a non-null value, some form of error occurs. Because empty and non-empty attributes are everywhere, it’s easy to make this kind of mistake.

Rust fundamentally solves this problem because Rust has no null values, but Rust has an enumeration that encodes concepts that exist or don’t exist. This enumeration is option, and option is defined in the standard library, as shown in the following code example:

enum Option<T> {
    some(T),
    None
}
Copy the code

The

syntax, which will be covered in future articles, is a generic type parameter, option

is a regular enumeration, and Some(T) and None are members of option

.


All you need to know in this article is that

means that Some members of the Option enumeration can contain any type of data. For example:

fn main() {
    let some_number = Some(5);
    let some_string = Some("a string");

    let absent_number: Option<i32> = None;
}
Copy the code

If you use None instead of Some, you need to tell Rust Option

what type it is, because the compiler cannot infer the type of the value held by the Some member from the None value alone.

When there is a value of Some, we know that there is a value, and that value is stored in Some. When there is a value of None, it has the same meaning as a null value in a sense: there is no valid value. So why is Option

better than null? In short, because Option

and T (where T can be any type) are different types, the compiler does not allow a comparison or operation between a value that is definitely non-null and a value that is definitely non-null, as in the following code example:

fn main() {
    let x: i8 = 5;
    let y: Option<i8> = Some(5);

    let sum = x + y;
}
Copy the code

If the above code is run, the following exception is thrown:

error[E0277]: the trait bound `i8: std::ops::Add<std::option::Option<i8>>` is
not satisfied
 -->
  |
5 |     let sum = x + y;
  |                 ^ no implementation for `i8 + std::option::Option<i8> ` |Copy the code

An exception means that Rust does not know how to add Option

to i8 because they are of different types. When you have a value of type i8 in Rust, the compiler confirms that it always has a valid value. We don’t have to worry about using it confidently without doing a short value check. It is only when Option

(or whatever type is used) that you need to worry about not having a value.

In other words, Option

must be converted to T before operating on T. One of the most common problems that usually helps us catch null values is assuming that a value is not null when it is.

If a null value is possible in the program, it must be realistically placed in Option

of the corresponding type. When using this value, you must explicitly handle null cases. This is a deliberate design decision by Rust to limit the proliferation of null values to increase the security of Rust code.

conclusion

The article was first published in the wechat public account Program Yuan Xiaozhuang, at the same time in nuggets.

The code word is not easy, reprint please explain the source, pass by the little friends of the lovely little finger point like and then go (╹▽╹)