Answers for "while loop in rust"

2

rust while loop

//from https://doc.rust-lang.org/rust-by-example/flow_control/while.html

fn main() {
    // A counter variable
    let mut n = 1;
    // Loop while `n` is less than 101
    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);
        }
        // Increment counter
        n += 1;
    }
}
Posted by: Guest on February-25-2021
0

loop in rust

#![allow(unused_variables)]
fn main() {
let mut x = 5; // mut x: i32
let mut done = false; // mut done: bool

while !done {
    x += x - 3;

    println!("{}", x);

    if x % 5 == 0 {
        done = true;
    }
}
}
Posted by: Guest on June-05-2020

Browse Popular Code Answers by Language