Option
We have already seen some use of Option<T>. It stores either a value of type T or nothing. For example, String::find returns an Option<usize>.
1. ```
fn main() {
```
2. ```
let name = "Löwe 老虎 Léopard Gepardi";
```
3. ```
let mut position: Option<usize> = name.find('é');
```
4. ```
println!("find returned {position:?}");
```
5. ```
assert_eq!(position.unwrap(), 14);
```
6. ```
position = name.find('Z');
```
7. ```
println!("find returned {position:?}");
```
8. ```
assert_eq!(position.expect("Character not found"), 0);
```
9. ```
}
```
This slide should take about 10 minutes.
Optionis widely used, not just in the standard library.-
unwrapwill return the value in anOption, or panic.expectis similar but takes an error message. -
You can panic on None, but you can’t “accidentally” forget to check for None.
- It’s common to
unwrap/expectall over the place when hacking something together, but production code typically handlesNonein a nicer fashion. - The “niche optimization” means that
Option<T>often has the same size in memory asT, if there is some representation that is not a valid value of T. For example, a reference cannot be NULL, soOption<&T>automatically uses NULL to represent theNonevariant, and thus can be stored in the same memory as&T.
