使用 ? 对 option 求值
2023-03-24 18:06 作者:skylon2001 | 我要投稿
使用 match
语句可以解开 Option
,但使用 ?
运算符通常会更容易。
比如:
fn get_birth_day(cur_age: Option<u8>) -> Option<String> {
// get the real value, wrapped by some
// if cur_age is None, below line x will be None
let x = cur_age?;
Some(format!("cur age is {}", x))
}
fn main() {
let _ = get_birth_day(Some(8));
}

self.job?.phone_number?.area_code
这种代码, 就可以避免使用match 进行很多层判断