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

Apart from the fact that he can reason about uninitialized values ​​set in the body loop

, are there any other compelling reasons for existence loop

?

+3


source to share


1 answer


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



See an example loop

on the Rust playground
and the same example withwhile true

. The generated assembly is exactly the same. The compiler gives a warning for while true

-example to use loop

instead.

+1


source







All Articles