zl程序教程

您现在的位置是:首页 >  前端

当前栏目

【Rust】while 循环

循环rust while
2023-09-27 14:23:46 时间

环境

  • Rust 1.56.1
  • VSCode 1.60.2

概念

参考:https://doc.rust-lang.org/stable/rust-by-example/flow_control/while.htmll

while 循环在条件为真的时候,一直执行,直到为假。

示例

while 循环

fn main() {
    let mut n = 1;
    while n < 101 {
        if n % 15 == 0 {
            println!("fizzbuzz");
        } else if n % 3 == 0 {
            println!("fizz");
        } else if n % 5 == 0 {
            println!("buzz");
        } else {
            println!("{}", n);
        }
        n += 1;
    }
}

总结

了解了 Rust 中的 while 循环。

附录