Rust Programming By Example
上QQ阅读APP看书,第一时间看更新

Creating while loops

There are multiple kinds of loop in Rust. One of them is the while loop.

Let's see how to compute the greatest common pisor using the Euclidean algorithm:

let mut a = 15;
let mut b = 40;
while b != 0 {
    let temp = b;
    b = a % b;
    a = temp;
}
println!("Greatest common pisor of 15 and 40 is: {}", a);

This code executes successive pisions and stops doing so when the remainder is 0.