Programming-Idioms

History of Idiom 78 > diff from v29 to v30

Edit summary for version 30 :

Version 29

2015-09-05, 13:25:06

Version 30

2015-09-05, 13:54:27

Idiom #78 "do while" loop

Execute a block once, then execute it again as long as boolean condition c is true.

Idiom #78 "do while" loop

Execute a block once, then execute it again as long as boolean condition c is true.

Code
loop {
    doStuff();
    if !c { break; }
}
Code
loop {
    doStuff();
    if !c { break; }
}
Comments bubble
Rust has no do-while loop with syntax sugar. Just use loop and break.
Comments bubble
Rust has no do-while loop with syntax sugar. Use loop and break.
Demo URL
https://play.rust-lang.org/?code=fn%20main()%20%7B%0A%20%20%20%20let%20mut%20i%20%3D%205%3B%0A%09loop%20%7B%0A%09%09println!(%22%7B%7D%22%2C%20i)%3B%0A%0A%09%09if%20i%20%25%202%20%3D%3D%200%20%7B%20i%20%2F%3D%202%3B%20%7D%0A%09%09else%20%7B%20i%20%3D%203*i%20%2B%201%3B%20%7D%0A%09%09%0A%09%09if%20i%20%3D%3D%201%20%7B%20break%3B%20%7D%0A%20%20%20%20%7D%0A%20%20%20%20%0A%20%20%20%20println!(%22And%20final%20value%3A%20%7B%7D%22%2C%20i)%3B%0A%7D%0A&version=stable
Demo URL
https://play.rust-lang.org/?code=fn%20main()%20%7B%0A%20%20%20%20let%20mut%20i%20%3D%205%3B%0A%09loop%20%7B%0A%09%09println!(%22%7B%7D%22%2C%20i)%3B%0A%0A%09%09if%20i%20%25%202%20%3D%3D%200%20%7B%20i%20%2F%3D%202%3B%20%7D%0A%09%09else%20%7B%20i%20%3D%203*i%20%2B%201%3B%20%7D%0A%09%09%0A%09%09if%20i%20%3D%3D%201%20%7B%20break%3B%20%7D%0A%20%20%20%20%7D%0A%20%20%20%20%0A%20%20%20%20println!(%22And%20final%20value%3A%20%7B%7D%22%2C%20i)%3B%0A%7D%0A&version=stable