使用 ? 對(duì) option 求值
2023-03-24 18:06 作者:skylon2001 | 我要投稿
使用?match
?語(yǔ)句可以解開?Option
,但使用??
?運(yùn)算符通常會(huì)更容易。
比如:
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 進(jìn)行很多層判斷