美文网首页
Rust 入门 - 控制流

Rust 入门 - 控制流

作者: Lee_dev | 来源:发表于2021-05-26 12:09 被阅读0次

    if

    let a = 8;
    if a == 8 {
        println!("a = 8");
    } else {
        println!("a != 8");
    }
    

    if 表达式

    let sw = true;
    let a = if sw { 6 } else { 8 };
    println!("a = {}", a);
    

    loop

    let mut index = 0;
    loop {
        if index == 10 {
            break;
        }
        index += 1;
    }
    println!("index = {}", index);
    

    loop 表达式

    let mut index = 0;
    let res = loop {
        if index == 10 {
            break index * 10;
        }
        index += 1;
    };
    println!("index = {}", index);
    println!("res = {}", res);
    

    while

    let mut index = 0;
    while index < 10 {
        index += 1;
        println!("index = {}", index);
    }
    println!("index = {}", index);
    

    数组遍历

    let nums: [u32; 5] = [1, 2, 3, 4, 5];
    for n in &nums {
        println!("n = {}", n);
    }
    
    for n in nums.iter() {
        println!("n = {}", n);
    }
    

    相关文章

      网友评论

          本文标题:Rust 入门 - 控制流

          本文链接:https://www.haomeiwen.com/subject/luaqsltx.html