rust Option枚举

枚举

 1 fn main() {
 2     let a_binding;
 3     {
 4         let x = 2;
 5         a_binding = x * x;
 6     }
 7     println!("a binding: {}", a_binding);
 8 
 9     let sn = Some(5);
10     let ss = Some("a string");
11     let absn: Option<i32> = None;
12     println!("{:?}:{:?}:{:?}",sn,ss,absn);
13 
14     fn ps1(n: Option<i32>) -> Option<i32> {
15         match n {
16             None => None,
17             Some(i) => Some(i+1), // 这里如果写作Some(i)+1,会出错,Some(i)为Option<i32>类型,1是<i32>类型
18         }
19     }
20     let six=ps1(sn);
21     let none=ps1(absn);
22     println!("{:?}:{:?}",six,none);
23 }

 

posted @ 2020-12-11 15:41  天使不设防  阅读(276)  评论(0编辑  收藏  举报