What does this number mean at the end?


#1

Hi. I have this code:

fn main() {
    let x: u8 = 260;

    println!("{}", x);
}

What does this number (4) mean at the end?

warning: literal out of range for u8
 --> src\main.rs:2:17
  |
2 |     let x: u8 = 260;
  |                 ^^^
  |
  = note: #[warn(overflowing_literals)] on by default

    Finished dev [unoptimized + debuginfo] target(s) in 0.40s
     Running `target\debug\learn_rust.exe`
4

P. S. Yes, I know, what 260 out of range for u8. Thanks!


#2

4 is remainder of 260 divided by 256 which is 2^8.


#3

Hmm, I thought so, but how can it help?


#4

When you run your program, your program prints “4”, because it stores the lowest 8 bits of 260 and then prints them out.


#5

That’s the output of your program, not part of the warning message. (If that’s what you’re confused about.)