Is it expected that too big a bit-shift will be undefined in Rust?

When running this code:

#![allow(exceeding_bitshifts)]

fn main() {
    const NUMBER: u64 = 0b_10101010;

    fn print_shift(i: u32) {
        println!("{:b}", NUMBER >> i);
    }

    print_shift(65);
    println!("{:b}", NUMBER >> 65);
}

You see that shifting the bits of a number with a value longer than the bit length causes a different behavior when executed at compile time or at run time.

Is this normal behavior? Is this documented somewhere? This is not on the list of documented behavior undefined .

+4
source share
1 answer

No, this is not , but it is not undefined behavior . This is a “simple” mistake .

, . , , , , , .


debug vs release, " " -, undefined. :

, undefined:

Rust , () , .

  • ...

. :

+6

Source: https://habr.com/ru/post/1687387/


All Articles