書名: Rust Programming By Example作者名: Guillaume Gomez Antoni Boucher本章字數: 83字更新時間: 2021-07-02 19:12:57
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.