Skip to content

Minimum signed integer literal #779

Closed
@Coder-256

Description

@Coder-256

In the section about integer literals, the Reference says:

Note that the Rust syntax considers -1i8 as an application of the unary minus operator to an integer literal 1i8, rather than a single integer literal.

However, this is a bit misleading due to the way Rust handles literals that are "out of range": values like -128i8 actually work (due to const eval?). AFAIK you can use any integer literal in the range [0, 2^128) as long as it evaluates to something in range, but either way things get a bit weird. For example:

Playground

fn main() {
    println!("{}", -128i8); // prints -128
    println!("{}", -(128i8)); // prints -128
    // println!("{}", -(126i8 + 2)); // error: attempt to add with overflow
    // let x = 128i8; // error: literal out of range for `i8`
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions