There are many methods can be used to check if an Option
has value. For examples:
Use is_some()
or is_none()
:
if x.is_some() {
println!("test_with_if i is {:?}", x.unwrap());
}
Use match
:
match x {
None => {}
Some(i) => {
println!("test_with_match i is {:?}", i);
}
}
Use if let
:
if let Some(i) = x {
println!("test_with_if_let i is {:?}", i);
};
Or if ok_or
to convert it to Result
type and then process as Result
:
let _ = x.ok_or(NotFoundError).and_then(|i| {
println!("test_with_ok_or i is {:?}", i);
Ok(i)
});
すべてのコードは ここ