Rust arithmetic overflow

It's because of the way the macro is expanded.

println!("{}",225u8+255u8)

expands the argument as

&(225u8+255u8)

Interestingly you can do this and get the same behavior:

let x = &(225u8+255u8);

So that then begs the question of why is taking the reference runtime vs compile time?

Edit:
For clarity, 'runtime vs compile time' might be a bit misleading. In Debug mode, this is compile time checked just like any other arithmetic operation. In Release mode, or with

overflow-checks=false

it is not. This makes it the same as any other runtime evaluated expression.
So the question is more like "Why isn't this treated as a compile time constant expression?"

1 Like