Rust Reference ยท Primitive Types
Rust signed integer types
Reference for Rust's signed integer types i8 to i128 and isize: ranges, literals, overflow behavior, common methods like abs, signum and rem_euclid.
Rust has six signed integer types: i8, i16, i32, i64, i128 and isize. The number is the width in bits; isize matches the pointer width of the target (64 bits on most desktops and servers). Values are stored in two's complement, so an iN holds every whole number from -2^(N-1) to 2^(N-1) - 1.
If nothing else constrains the type, an integer literal defaults to i32. Arithmetic that overflows panics in debug builds and wraps in release builds by default, so use the explicit checked_, wrapping_, saturating_ and overflowing_ families when overflow is possible.
let a: i8 = -128; let b = 1_000_000; // i32 let c = 42_i64; let d = 0xFF_i16;
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Syntax
let n: i32 = -5; const LIMIT: i64 = i64::MAX; let parsed: Result<i32, _> = "-12".parse::<i32>();
| Type | Bits | Minimum | Maximum |
|---|---|---|---|
i8 |
8 | -128 | 127 |
i16 |
16 | -32,768 | 32,767 |
i32 |
32 | -2,147,483,648 | 2,147,483,647 |
i64 |
64 | -9.22 x 10^18 | 9.22 x 10^18 |
i128 |
128 | -1.7 x 10^38 | 1.7 x 10^38 |
isize |
pointer width | depends on target | depends on target |
Common associated items: MIN, MAX, BITS, abs(), signum(), pow(), abs_diff(), unsigned_abs(), rem_euclid(), div_euclid(), is_positive(), is_negative(), count_ones(), leading_zeros(), from_str_radix().
Panics: arithmetic overflow (debug builds), division or remainder by zero (always), i32::MIN / -1, and abs()/negation of MIN (debug builds). Parsing returns Err(ParseIntError) instead of panicking.
Usage notes
- Default type. An unconstrained integer literal is
i32; a literal suffix (5_u8) or annotation overrides it. - Use
isizeonly for offsets and pointer-sized math, andusizefor indexes and lengths. Do not useisizeas a "big integer". /truncates toward zero and%takes the dividend's sign. For a mathematical modulo userem_euclid, which is never negative for a positive divisor.- Casting with
aswraps silently (300_i32 as i8is 44). Usei8::try_from(x)for checked conversion, andi64::from(x)for lossless widening. i128is slower on most CPUs because it needs two registers; use it only wheni64is too small (factorials, big sums).- No implicit conversions.
i32 + i64is a type error; convert one side first. - Hashing and ordering. All integer types implement
Eq,Ord,HashandCopy, so they work as map keys.