liveBook · Manning

Integer

As explained in Rust in Action: Systems programming concepts and techniques

An integer is a whole-number value without a fractional component.

An integer is a whole-number value without a fractional component. In Rust, `20` is an integer literal; `20.0` is a floating-point literal. You can let Rust determine a literal’s type, annotate a variable as in `let count: i32 = 20`, or add a suffix as in `20i32`. Conversions between numeric types are explicit. section 2.3.1

Choosing an integer type means choosing what values it can hold:

  • Signed types such as `i8` and `i32` represent negative and positive values. Unsigned types such as `u8` and `u32` represent nonnegative values. The number names the type’s width in bits; `isize` and `usize` use the CPU’s native width. section 2.3.2
  • A wider type uses more bits in memory and the CPU, allowing a wider range of values. Unlike a floating-point number, an integer cannot give up precision to extend its range. section 2.3.2 section 5.2
  • Write decimal literals normally, or use `0b`, `0o`, and `0x` for binary, octal, and hexadecimal. For example, `0b11`, `0o36`, and `0x12C` represent 3, 30, and 300. section 2.3.2

What happens at the limit?

A `u16` has 16 bits and holds values from 0 through 65,535. Incrementing beyond its range causes integer overflow: the next value cannot fit. In the shown loop, adding 1,000 repeatedly eventually produces an overflow panic with default `rustc` options. Try compiling it with `rustc -O` too; its behavior differs. This is why a counter’s type needs a range large enough for the values it may reach. section 5.2

Integers can also serve as simple identifiers: three mock satellites are represented by `0`, `1`, and `2` and passed to a status-checking function that accepts `u64`. At a lower level, an integer type such as `u8` can prescribe how an enum’s variants are represented when an external system requires a particular layout. section 4.1 section 11.5.2

How Rust in Action: Systems programming concepts and techniques covers it

How deep it goes
Explains literals, widths, bit patterns, and overflow through runnable examples rather than a comprehensive numeric-type reference.
Examples
Uses base-prefixed literals, a `u16` incrementing loop, and integer IDs passed to a satellite status function.
What it assumes
Starts with familiar decimal numerals before introducing Rust annotations, literal suffixes, and alternative bases.
What you can do after
Diagnose/debug integer overflow and choose types whose ranges accommodate expected values.
How it connects
Connects integers to floating-point numbers, numeric type conversions, binary representation, integer overflow, and enum discriminants.

Also explained in

Compare how 15 books explain integer →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Rust in Action: Systems programming concepts and techniques. The section links point to the parts of the book it draws on.