Option is similar to the null
of other languages, except it is type safe.
You must transform an Option before you can use it.
You may see it like this Option<usize>
or like this Option<Vec<u32>>
or maybe like this std::Option<()>
, or some other way – the possibilities are endless. Here’s some examples of how you can use an option…
Examples
Create an option and consume it
fn main() {
// create an option with a value
let number_option = Option::Some(32);
// crash with an error message if the option is empty
let number = number_option.unwrap();
println!("{}", number);
}
Create an empty option and consume it
fn main() {
// create it manually without a value
let number_option: Option<u32> = Option::None;
// crash with an error message if the option is empty
let number = number_option.unwrap();
println!("{}", number);
}
Get an option from a function and consume it
fn main() {
// create 2 numbers with 8 bits in size
let number1: u8 = 50;
let number2: u8 = 10;
// use a function that can fail (this one fails if the result can't fit into 8 bits)
let number3_option = number1.checked_mul(number2);
// choose to crash if the function failed
let number3 = number3_option.unwrap();
println!("{}", number3);
}
Get an option from a function and print it
// get an option from a function and print it
fn main() {
let letters = "😍";
// will faill if t is not in letters
let position = letters.find("t");
// debug print the option, without crashing
println!("position is '{:?}'", position);
}
Get an option from a function and consume it better
fn main() {
let my_string = "Öl".to_string();
// Can fail if the 0th and 1st byte are do not make up a complete character.
let slice_option = my_string.get(0..1);
// give a nice error message if it fails
let slice = slice_option.expect("Failed to get first bytes");
println!("the first character is '{}'", slice);
}
Do something different if it’s empty
fn main() {
let list = vec!['m', 'e', 'o', 'w'];
let tenth_option = list.get(10);
match tenth_option {
None => {println!("There was no tenth :(");}
Some(tenth) => {println!("The tenth character is {:?}", tenth)}
}
}
Further reading
- The explanation of why for Option, https://doc.rust-lang.org/book/ch06-01-defining-an-enum.html?highlight=option#the-option-enum-and-its-advantages-over-null-values
- API Documentation for Option https://doc.rust-lang.org/std/option/enum.Option.html
- Rustlings, exercise 12 – how to do it, in the readme , full project , exercise 12 source
- Rust by example, free online book. Part 19.4 https://doc.rust-lang.org/rust-by-example/std/option.html?highlight=option#option
59 responses to “Rust: The heck is an option<> supposed to mean?”
I have your nose.
https://forum.stde.ru/index.php?/gallery/image/829-16/
http://terios2.ru/forums/index.php?autocom=gallery&req=si&img=4605
http://terios2.ru/forums/index.php?autocom=gallery&req=si&img=4608
http://toyota-porte.ru/forums/index.php?autocom=gallery&req=si&img=3338
http://terios2.ru/forums/index.php?autocom=gallery&req=si&img=4760
Awesome https://is.gd/tpjNyL
Good https://is.gd/tpjNyL
Good https://shorturl.at/2breu
Good https://shorturl.at/2breu
Good https://shorturl.at/2breu
Awesome https://lc.cx/xjXBQT
Awesome https://lc.cx/xjXBQT
http://passo.su/forums/index.php?autocom=gallery&req=si&img=4296
http://passo.su/forums/index.php?autocom=gallery&req=si&img=4296
Good https://t.ly/tndaA
Very good https://t.ly/tndaA
Very good https://t.ly/tndaA
Very good https://t.ly/tndaA
Awesome https://t.ly/tndaA
Good https://t.ly/tndaA
Awesome https://urlr.me/zH3wE5
Awesome https://is.gd/N1ikS2
Awesome https://is.gd/N1ikS2
http://wish-club.ru/forums/index.php?autocom=gallery&req=si&img=5490
https://mazda-demio.ru/forums/index.php?autocom=gallery&req=si&img=6600
Good https://is.gd/N1ikS2
Awesome https://is.gd/N1ikS2
Awesome https://is.gd/N1ikS2
Very good https://is.gd/N1ikS2
Good https://is.gd/N1ikS2
Very good https://is.gd/N1ikS2
Very good https://is.gd/N1ikS2
Very good https://is.gd/N1ikS2
Awesome https://is.gd/N1ikS2
Awesome https://is.gd/N1ikS2
Very good https://is.gd/N1ikS2
Very good https://is.gd/N1ikS2
Very good https://is.gd/N1ikS2
Good https://is.gd/N1ikS2
Very good https://is.gd/N1ikS2
Awesome https://is.gd/N1ikS2
Very good https://is.gd/N1ikS2
Good https://is.gd/N1ikS2
Very good https://is.gd/N1ikS2
Very good https://is.gd/N1ikS2
Awesome https://is.gd/N1ikS2
https://vitz.ru/forums/index.php?autocom=gallery&req=si&img=4918
http://wish-club.ru/forums/index.php?autocom=gallery&req=si&img=5340
https://honda-fit.ru/forums/index.php?autocom=gallery&req=si&img=7192
http://wish-club.ru/forums/index.php?autocom=gallery&req=si&img=5394
https://vitz.ru/forums/index.php?autocom=gallery&req=si&img=4897
http://terios2.ru/forums/index.php?autocom=gallery&req=si&img=4642
https://honda-fit.ru/forums/index.php?autocom=gallery&req=si&img=7139
https://honda-fit.ru/forums/index.php?autocom=gallery&req=si&img=7144
https://cr-v.su/forums/index.php?autocom=gallery&req=si&img=4030
https://honda-fit.ru/forums/index.php?autocom=gallery&req=si&img=7053
https://vitz.ru/forums/index.php?autocom=gallery&req=si&img=4854
https://hrv-club.ru/forums/index.php?autocom=gallery&req=si&img=6913