Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

The Integer Types are available in signed and unsigned flavours, from 8 bits to 128 bits. The unsigned versions are: u8, u16, u32, u64 and u128. The signed versions are: i8, i16, i32, i64 and i128.

Table 1 provides a list of available operators.

Table 1:Arithmetic operators

Operator

Description

+

Addition

-

Subtraction

*

Multiplication

/

Division [1] [2]

%

Remainder [3]

6.1.1Default type

If unspecified, an integer is of type i32:

15

6.1.2Specifying the type

To set explicitly the integer type, we can either set it to the variable or the value:

(27, 516)

6.1.3Separator

For big literal numbers, the underscore can be used to ease reading:

(1000000000, 123456)

6.1.4Binary, octal and hexadecimal

Binary, octal and hexadecimal numbers can be defined using the appropriate prefix (0b, 0o, 0x):

(1054, 310, 603949512)

6.1.5Size types

Two architecture dependent integer types exist to store sizes (e.g.: array sizes, string sizes). One is signed and the other unsigned:

(-1, 8000)

6.1.6Character byte

A special notation exists to define a character byte, which is stored as a plain 8-bit integer (u8):

65

The type of n will be u8.

6.1.7Operations

Examples of computing a remainder:

(2, -2, 2, -2)

A division:

(3, -3, -3, 3)

Some constants are directly accessible on the primitive types: i8, i16, i32, i64, u8, u16, u32, u64.

Example for 8-bit unsigned integer:

BITS: 8
MIN: 0
MAX: 255

Example for 32-bit unsigned integer:

BITS: 32
MIN: 0
MAX: 4294967295

Example for 64-bit signed integer:

BITS: 64
MIN: -9223372036854775808
MAX: 9223372036854775807
Footnotes
  1. Rounded towards zero.

  2. Division by zero panics.

  3. Rust uses a remainder defined with truncating division. Given remainder = dividend % divisor, the remainder will have the same sign as the dividend.