Option and Result
Option Definition
enum Option<T> {
None, // absence of Value
Some(T), // presemce of value
}Option is a container type in rust which is used to represent a value which may or may not be present. The compiler will treat an Option value as schrodinger's cat, meaning it does not assume it's presence or absence. So as a programmer we need to instruct the compiler on what to do in each of the case.
How to use an Option
Pattern matching
let v: Option<i32> = Some(5i32);
let z: i32 = v + 10; // FAILS to compileInstruct what to do on either case
match v {
Some(num) => num + 10,
None => ???,
}In case of missing value you can choose to terminate the program using panic! macro or choose a defualt value.
Panics
Default behaviour
Result Definition
Last updated