What does this number mean at the end?

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!

1 Like

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

1 Like

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

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

1 Like

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

10 Likes