Sub topic 7
Integer types and literals
This chapter assumes you can write and run a basic Rust program with cargo or rustc. You should also know how let bindings and type annotations work. Here you will learn which integer types Rust offers, how to write integer literals, and how the compiler picks a type when you don't name one.
The integer types
Rust has no single "int" type. Every integer type states its size and whether it can hold negative values. Signed types start with i and unsigned types start with u. The number is the width in bits.
| Width | Signed | Unsigned |
|---|---|---|
| 8 bits | `i8` | `u8` |
| 16 bits | `i16` | `u16` |
| 32 bits | `i32` | `u32` |
| 64 bits | `i64` | `u64` |
| 128 bits | `i128` | `u128` |
| pointer-sized | `isize` | `usize` |
A signed type with n bits holds values from -2^(n-1) to 2^(n-1) - 1. An unsigned type holds 0 to 2^n - 1. So i8 covers -128 to 127, and u8 covers 0 to 255.
isize and usize match the pointer width of the target machine: 64 bits on a 64-bit platform and 32 bits on a 32-bit one. Rust uses usize for indexes and lengths, so you will meet it whenever you index an array or call .len().
You can read the exact limits from the type itself:
fn main() {
println!("i8: {} to {}", i8::MIN, i8::MAX);
println!("u8: {} to {}", u8::MIN, u8::MAX);
println!("i32: {} to {}", i32::MIN, i32::MAX);
println!("u64 max: {}", u64::MAX);
println!("usize bits: {}", usize::BITS);
}Integer literals
A literal is a value written directly in source code. Rust supports several forms.
| Form | Example | Value |
|---|---|---|
| Decimal | `98_222` | 98222 |
| Hexadecimal | `0xff` | 255 |
| Octal | `0o77` | 63 |
| Binary | `0b1111_0000` | 240 |
| Byte (`u8` only) | `b'A'` | 65 |
Underscores are ignored and exist only to make long numbers readable. You can also attach a type to a literal as a suffix, such as 57u8 or 1_000i64.
fn main() {
let million = 1_000_000;
let mask = 0b1111_0000u8;
let color = 0xff_a0_00;
let letter = b'A';
let small = 57u8;
println!("{million} {mask} {color} {letter} {small}");
}How the compiler picks a type
If nothing constrains a literal, an integer literal defaults to i32. The compiler looks at the whole function, though, and uses any information it finds. A later use can fix the type of an earlier binding.
fn main() {
let a = 10; // no other hints, so i32
let b = 10; // used as u64 below, so u64
let c: u8 = 200; // annotation
let d = 5_i16; // suffix
let total: u64 = b + 1;
println!("{a} {total} {c} {d}");
println!("{}", std::mem::size_of_val(&a));
println!("{}", std::mem::size_of_val(&b));
}This prints sizes of 4 bytes for a and 8 bytes for b. The two bindings look identical, but b is used with a u64, so inference gives it that type.
Choosing a type
Use these rules in practice:
- Start with
i32unless you have a reason not to. It is the default and is usually fast. - Use
usizefor indexes, counts of items, and anything that measures memory. - Use
u8for raw bytes andu64ori64for values that may exceed about 2 billion, such as file sizes or timestamps. - Use the unsigned types only when a negative value makes no sense. Subtracting from an unsigned number can go below zero, which is an overflow (covered in a later chapter).
Common errors
Rust never converts between integer types silently. Mixing them is a compile error:
fn main() {
let a: i32 = 5;
let b: i64 = 10;
// let c = a + b; // error[E0308]: mismatched types
let c = i64::from(a) + b;
println!("{c}");
}A literal that does not fit its type is also rejected. let x: u8 = 256; fails with the lint overflowing_literals, which is deny-by-default and so stops compilation. The same applies to a negative literal assigned to an unsigned type, such as let y: u32 = -1;, which fails with a different error because unsigned types cannot be negated.
Another common surprise is an ambiguous type. "42".parse().unwrap() has no way to learn which integer you want, so you must write let n: u32 = "42".parse().unwrap(); or use parse::<u32>().
Exercise
What type does x have in let x = 3_000_000_000; if nothing else constrains it, and does the program compile?
Show answer
The default type is i32, but 3,000,000,000 exceeds i32::MAX (2,147,483,647). The overflowing_literals lint is deny-by-default, so compilation fails. Annotating let x: u32 = 3_000_000_000; or let x: i64 = ... fixes it.
Exercise
Which integer type does Rust use for array indexes, and why can let i: i32 = 2; arr[i] not compile?
Show answer
Array indexes use usize. Rust does not convert i32 to usize implicitly, so you must write arr[i as usize] or declare i as usize.