50.8.1Returning an Option¶
We define foo() as a function that takes a usize as argument and
returns an Option<bool>. If the indices is out-of-bound, we return a
None value:
fn foo(i: usize) -> Option<bool> {
let arr = [true, false, true];
if i >= arr.len() {
None
} else {
Some(arr[i])
}
}Here are the results for indices from 0 to 4:
(foo(0), foo(1), foo(2), foo(3), foo(4))(Some(true), Some(false), Some(true), None, None)