Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Choose the width of an integer

The built-in integer names specify their sizes:

TypesMeaning
i8, i16, i32, i64Signed integers of 8, 16, 32, or 64 bits
u8, u16, u32, u64Unsigned integers of those widths

For example, i8 can hold -128 through 127, and u8 can hold 0 through 255.

A whole-number literal without a suffix, such as 42, defaults to i64 when its type is unconstrained. An annotation, parameter, return type, or arithmetic operand can supply the type instead. Append a built-in integer type to choose another width:

def main() -> Result[(), IoError]:
    small: u8 = 200u8
    offset: i32 = -12i32
    large: u64 = 1_000_000u64
    print(small)?
    print(offset)?
    print(large)?
    Ok(())
200
-12
1000000

An annotation guides an unsuffixed literal, so small: u8 = 200 works. It does not convert an already typed initializer: convert the value explicitly for that.

Underscores separate digits for readability, as in 1_000_000u64 above. Integers may also be written in hexadecimal, octal, or binary, with the same suffixes and range checks:

def main() -> Result[(), IoError]:
    mask: u8 = 0b1111_0000
    print(0xFF, 0o755, mask, 0x1F600u32)?
    Ok(())
255 493 240 128512