Option and Result
The types represent optional data:
fn main() { let numbers = vec![10, 20, 30]; let first: Option<&i8> = numbers.first(); println!("first: {first:?}"); let arr: Result<[i8; 3], Vec<i8>> = numbers.try_into(); println!("arr: {arr:?}"); }
OptionandResultare widely used not just in the standard library.Option<&T>has zero space overhead compared to&T.Resultis the standard type to implement error handling as we will see on Day 3.try_intoattempts to convert the vector into a fixed-sized array. This can fail:- If the vector has the right size,
Result::Okis returned with the array. - Otherwise,
Result::Erris returned with the original vector.
- If the vector has the right size,