Rust while & if

简单的示例:

enum Foo{
    Zhangsan,
    Lisi,
    Wangwu(u8)
}



fn main() {
    let a = Foo::Zhangsan;
    let b = Foo::Lisi;
    let c = Foo::Wangwu(23);

    if let Foo::Zhangsan = a{
        println!("a is Foo::zhangsan");
    }

    if let Foo::Zhangsan = b{
        println!("b is Foo::Zhangsan"); // 此行不会打印
    }

    if let Foo::Wangwu(val) = c{
        println!("c is Foo::Wangwu = {}",val)
    }

    println!("==================================");

    let mut optional = Some(0);
    while let Some(i) = optional{
        if i > 5{
            println!("Greater than 5 ,quit!");
            optional = None;
        }else {
            println!("i is {:?} Try again.", i);
            optional = Some(i+1)
        }
    }
}
// a is Foo::zhangsan
// c is Foo::Wangwu = 23
// ==================================
// i is 0 Try again.
// i is 1 Try again.
// i is 2 Try again.
// i is 3 Try again.
// i is 4 Try again.
// i is 5 Try again.
// Greater than 5 ,quit!
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 1024 设计师:白松林 返回首页