January 2018
Beginner to intermediate
454 pages
10h 8m
English
To explain briefly how it works, when the Option type is Some, it simply means it contains a value whereas None doesn't. It has already been explained in Chapter 1, Basics of Rust, but here's a little recap just in case you need one. We can compare this mechanism with pointers in C-like languages; when the pointer is null, there is no data to access. The same goes for None.
Here's a short example:
fn divide(nb: u32, divider: u32) -> Option<u32> { if divider == 0 { None } else { Some(nb / divider) } }
So here, if the divider is 0, we can't divide or we'll get an error. Instead of setting an error or returning a complicated type, we just return an Option:
let x = divide(10, 3); let y = divide(10, 0);
Here, x is equal ...
Read now
Unlock full access