The integer types¶
Level: 101 → 201 · newcomer
Stub — an outline, not a lesson. There is no runnable example behind this page yet, so nothing on it has been through the check that backs every other claim in this library. The bullets below are the questions the finished page has to answer.
One line: Rust has twelve integer types — signed and unsigned at 8, 16, 32, 64 and 128 bits, plus isize and usize, which are as wide as a pointer — and an unannotated integer literal is an i32 unless something else in the program asks for a different type.
What it has to cover¶
- The table: each type's width and its
MINandMAX, printed by a program rather than typed usizeis the type of indexes and lengths, because it is as wide as an address; whyv[i]refuses ani32index and what the fix is- The default: why
let x = 5;is ani32, and how a later use can change the type inference picks - Choosing a type: the size of the value's range first, then
usizewhere the value indexes something, theni64oru64for counts that might grow - Overflow in one line each — a panic in a debug build, wrap-around in a release build — and the four method families (
checked_,wrapping_,saturating_,overflowing_) that make the choice explicit; the full story is on Meet the byte - Mixing types:
u8 + u32does not compile, and the conversions that make it compile belong to Conversion
The trap it exists for¶
Choosing u32 for a length "because it cannot be negative", then subtracting two of them. a - b with b > a panics in a debug build and wraps to a number near four billion in release, where the test that would have caught it did not run.
If you are coming from another language¶
- C and C++: the widths are fixed rather than platform-defined —
i32is 32 bits everywhere — andusizeissize_t. Signed overflow is not undefined behaviour here: it is a panic or a wrap, depending on the build, as Signed overflow shows. - Python:
inthas no width and never overflows; every Rust integer does, so the width is a decision you make. - Java: the same signed widths plus unsigned ones Java does not have, and overflow that is checked in debug builds instead of always wrapping.
See also¶
- Meet the byte —
u8, and the overflow that differs by build - Writing a number down — literal suffixes such as
5u64 - Tables 2.1 and 2.2, run — ten of the types printed with their ranges, and the targets where
usizeis not the CPU's width - Casting with
as— converting between these types without a check - The Reference: numeric types ↗
- Rust: The Practical Guide, chapter 2 exercises, run and chapter 3 exercises, run —
-1into au8isE0600,i16is the smallest type for-15,170and their product, and a square of a sum outgrows ani32at N = 304
Po polsku¶
Rust ma dwanaście typów całkowitych: ze znakiem (i8 … i128) i bez znaku (u8 … u128) oraz isize i usize, których szerokość równa się szerokości wskaźnika — dlatego usize jest typem indeksów i długości. Liczba bez adnotacji to domyślnie i32. Przepełnienie (overflow) nie jest tu zachowaniem niezdefiniowanym jak w C: w kompilacji debug kończy się paniką, w release zawinięciem wartości.
Szukaj po polsku: typy całkowite w Ruscie · przepełnienie liczb całkowitych · rust integer types usize · rust integer overflow debug release