UB can be confusing, but please remember it also means a lot of very reasonable optimization opportunities.
Say in data flow analysis, when the compiler can assume "(value + 1) > value" to be always true, a whole expensive branch can be removed.
If the overflow is never going to happen anyways, the savings are considerable (expensive branch eliminated) and the result is always correct.
Re:Myths about Integer Overflow in Rust
by Manishearth on Friday April 29, 2016 @06:03PM
(#11599062)
I know that UB can be useful for optimizations. Where did I assert otherwise?
I'm saying that overflow is not undefined in Rust (the very first section of the blog post says this). I made this same mistake a few days ago and thought it was undefined (and implementation-defined to panic on debug), but I was wrong. Overflow is well defined in Rust.
Is it? What do you mean by illegal here? Overflow can't cause UB madness in Rust like it does in C; the compiler does not optimize loops assuming no overflow.