sa___
sa___

Reputation: 363

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 ?

Upvotes: 4

Views: 467

Answers (1)

user2722968
user2722968

Reputation: 16475

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.

Upvotes: 1

Related Questions