«

Rust - 循环

ljierui 发布于 阅读:128 技术杂谈


1、循环

1.1、loop循环

fn main() {
    let mut number = 0;
    let result = loop {
        number += 1;
        if number == 10 {
            break number * 2;
        }
    };
}

1.2、while条件循环

fn main() {
    let mut number = 10;
    while number !=0 {
        println!("{}!",number);
        number = number - 1;
    }
    println!("结束");
}

1.3、for循环

fn main() {
    let number = [1,2,3,4,5,6,7,8];
    for elemnt in number.iter()  {
        println!("the value is {}",elemnt);
    }
}

rust语言学习

推荐阅读: