liveBook · Manning

Unsigned integer

As explained in Learn Rust in a Month of Lunches

An unsigned integer type represents nonnegative whole numbers without a sign.

An unsigned integer is a whole number that cannot be negative. In Rust, unsigned integer types include u8, u16, u32, u64, u128, and usize. By contrast, a signed integer can be negative, zero, or positive. section 1.3

The number after `u` tells you how many bits the type uses. A bit can be 0 or 1, and each bit position is worth twice as much as the previous one. More bits allow larger numbers: `u8` can hold values up to 255, while `u16` can hold values up to 65,535. section 1.3

fn main() {
    let my_number: u8 = 100;             #1
    println!("{}", my_number as char);
}

The `: u8` annotation tells Rust that `my_number` has the unsigned type `u8`. The example then casts that value to a character. section 1.3

How Learn Rust in a Month of Lunches covers it

How deep it goes
Defines unsigned values, lists type widths and limits, and explains how binary bits contribute to a number.
Examples
Uses the maximum values of u8 and u16 and a u8 variable cast to a character.
What it assumes
Introduces bits and binary digits from first principles and defines signed and unsigned in plain language.
What you can do after
Apply it via a Rust type annotation to declare a u8 value and recognize its 255 maximum.
How it connects
Links unsigned integers to signed integers, bits, binary numbers, bytes, and character casts.

Also explained in

Compare how 9 books explain unsigned 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.