Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What optimizations does the Rust compiler make with `loop` versus `while true`?

Aside from the fact that it can reason about uninitialized values set within the loop body, are there any other compelling reasons for loop to exist ?

like image 285
sa___ Avatar asked Dec 04 '25 09:12

sa___


1 Answers

Other than you state your intention, there is no difference. All loops are the same once normalization has happened in the compiler.

See an example of a loop in the Rust playground and the same example with a while true. The assembly generated is exactly the same. The compiler gives a warning for the while true-example to use loop instead.

like image 152
user2722968 Avatar answered Dec 07 '25 21:12

user2722968