Option
可以用来给有可能为空的值作为初始化
struct A
{
a:Option<T>
}
这样的a初始化的时候可以给NONE
如果不为空的话
需要Some包裹一下
因为Option是枚举,具体是长这样的
enum Option<T> {
Some(T),
None,
}
if let
Some除了match来解析之外,如果你期望他是某个具体类型可以用这个
这个例子就是具体到希望some_test的值是5
加起来试试
struct A {
a: Option<i32>,
}
// let test_struct = A { a: None };
let test_struct = A { a: Some(1) };
if let Some(1) = test_struct.a {
println!("666");
} else {
println!("none")
}