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 }
作者:天使不设防
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.