Rust 编程视频教程对应讲解内容-流程控制

视频地址

头条地址: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!");
}
本作品采用《CC 协议》,转载必须注明作者和本文链接

令狐一冲

讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!