liveBook · Manning

Pointer-sized integer

As explained in Learn Rust in a Month of Lunches

A pointer-sized integer is a Rust signed or unsigned integer whose width matches the computer architecture.

Rust has two pointer-sized integer types: `isize` and `usize`. Their width matches the computer architecture rather than a fixed number of bits. `isize` is signed, so it can represent negative numbers; `usize` is unsigned, so it cannot. Both hold whole numbers, just like fixed-width types such as `i32` and `u8`. section 1.3

A practical use for `usize` is working with arrays. Rust uses it to index arrays, and an array length can be expressed as a `usize` const generic. That lets one struct definition work with different array lengths. section 16.1

struct Buffers<T, const N: usize> {
    array_one: [T; N],
    array_two: [T; N]
}

Here `N` is a `usize` length. Both arrays have length `N`, while `T` determines their element type. section 16.1

How Learn Rust in a Month of Lunches covers it

How deep it goes
Defines the types within an integer survey and demonstrates `usize` in a struct; does not calculate architecture-specific widths.
Examples
Shows a struct with `const N: usize` and two arrays, then creates instances with lengths three and four.
What it assumes
Introduces integer signedness from basics; assumes familiarity with structs and generics for the array example.
What you can do after
Apply it via Rust's `usize` to declare a const-generic array length.
How it connects
Connects signed and unsigned integers, bit widths, arrays, and const generics.

Also explained in

Compare how 2 books explain pointer-sized integer →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Rust in a Month of Lunches. The section links point to the parts of the book it draws on.