Logo

Programming-Idioms

History of Idiom 78 > diff from v73 to v74

Edit summary for version 74 by programming-idioms.org:
Restored version 72

Version 73

2020-06-26, 04:47:59

Version 74

2020-06-27, 10:17:04

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.

Variables
c
Variables
c
Extra Keywords
until
Extra Keywords
until
Code
while {
    doStuff();
    !c
} {}
Code
loop {
    doStuff();
    if !c { break; }
}
Comments bubble
Rust has no do-while loop with syntax sugar. 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
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