Rust 程式設計視訊教程對應講解內容-流程控制

linghuyichong發表於2019-12-19

頭條地址:https://www.ixigua.com/i676544267458235648...
B站地址:https://www.bilibili.com/video/av78062009?...
網易雲課堂地址:https://study.163.com/course/introduction....

fn main() {
    //if
    let y = 1;
    if y == 1 {
        println!("y = 1");
    }

    //if-else
    if y == 1 {
        println!("y = 1");
    } else {
        println!("y != 1");
    }

    //if - else if - else
    if y == 0 {
        println!("y = 0");
    } else if y == 1 {
        println!("y = 1");
    } else {
        println!("y != 1");
    }

    //在let中使用if
    let condition = true;
    let x = if condition {
        5
    } else {
        6
        //"six" //error
    }; 
    println!("x = {}", x);

    //loop
    //loop {
    //    println!("in loop");
    //}

    let mut counter = 0;
    let result = loop {
        counter += 1;
        if counter == 10 {
            break counter * 2;
        }
    };
    println!("The result is {}", result);

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

    //for 
    let arr = [1, 2, 3, 4];
    for element in arr.iter() {
        println!(" element = {}", element);
    }

    println!("Hello, world!");
}

令狐一衝

相關文章