Skip to content

Assinging to an i32 variable a binary representation of i32::MIN fails to compile #107896

Closed
@psvri

Description

@psvri

I tried this code:

fn main() {
    let x: i32 = 0b1000_0000_0000_0000_0000_0000_0000_0000i32;
    println!("{}", x);
}

playground link

I expected the code to compile since its i am assigning into x binary representation of i32::MIN, instead the compiler throws an error.

Meta

rustc --version --verbose:

1.67.1
Backtrace

   Compiling playground v0.0.1 (/playground)
error: literal out of range for `i32`
 --> src/main.rs:2:18
  |
2 |     let x: i32 = 0b1000_0000_0000_0000_0000_0000_0000_0000i32;
  |                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using the type `u32` instead: `0b1000_0000_0000_0000_0000_0000_0000_0000u32`
  |
  = note: the literal `0b1000_0000_0000_0000_0000_0000_0000_0000i32` (decimal `2147483648`) does not fit into the type `i32` and will become `-2147483648i32`
  = note: `#[deny(overflowing_literals)]` on by default

error: could not compile `playground` due to previous error

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-enhancementCategory: An issue proposing an enhancement or a PR with one.E-help-wantedCall for participation: Help is requested to fix this issue.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions