mirror of
https://github.com/google/comprehensive-rust.git
synced 2024-12-01 02:42:20 +02:00
Clarify what "undefined behavior" means in arithmetic.md (#2139)
Undefined behavior does not just mean that the behavior can be different on different platforms. It is much worse than that: the runtime behavior can be completely nonsensical since the whole program has undefined behavior. The compiler will optimizer as if there is no undefined behavior and will can lead to strange situations as the compiler concludes that things like `a > a + 1000` can never be reached if `a` is a signed 8-bit value (since reaching it would trigger undefined behavior).
This commit is contained in:
parent
4c61cafda8
commit
c45b291851
@ -23,8 +23,8 @@ be covered in more detail later.
|
||||
Arithmetic is very similar to other languages, with similar precedence.
|
||||
|
||||
What about integer overflow? In C and C++ overflow of _signed_ integers is
|
||||
actually undefined, and might do different things on different platforms or
|
||||
compilers. In Rust, it's defined.
|
||||
actually undefined, and might do unknown things at runtime. In Rust, it's
|
||||
defined.
|
||||
|
||||
Change the `i32`'s to `i16` to see an integer overflow, which panics (checked)
|
||||
in a debug build and wraps in a release build. There are other options, such as
|
||||
|
Loading…
Reference in New Issue
Block a user