liveBook · Manning

Primitive data type

As explained in Learn Rust in a Month of Lunches

A primitive type is a built-in Rust type representing a basic value such as a number or Boolean.

A primitive type is one of Rust’s simplest types. Integers and characters are two examples. Even these basic types involve choices: an integer’s type determines whether it can be negative and how large it can grow. section 1.3

Rust’s integer types represent whole numbers, with no decimal point:

  • Signed types can hold negative numbers, zero, or positive numbers. Their names begin with i: i8, i16, i32, i64, i128, and isize. section 1.3
  • Unsigned types hold only nonnegative numbers. Their names begin with u: u8, u16, u32, u64, u128, and usize. section 1.3
  • In names such as u8 and u16, the number gives the number of bits. More bits allow larger values: u8 holds up to 255, while u16 holds up to 65,535. section 1.3

A bit is a place that holds either 0 or 1. Eight bits make one byte. Thinking about those bits helps explain why Rust offers several types for the same everyday idea of a whole number. section 1.3

A character and a type correction

A character can be a letter, a space, or a symbol: `let first_letter = 'A';` and `let cat_face = '😺';` are both valid examples. Type matters when converting a number to a character. If `let my_number = 100;` is followed by `my_number as char`, the compiler reports that it cannot cast the inferred `i32` directly to `char`; the conversion requires `u8`. Writing `let my_number: u8 = 100;` makes the intended type explicit. section 1.3

fn main() {
    let str1 = "Hello!";
    println!("str1 is {} bytes and also {} characters.", str1.len(), str1.chars().count());
    let str2 = "안녕!";
    println!("str2 is {} bytes but only {} characters.", str2.len(), str2.chars().count());
}

Compare `.len()` with `.chars().count()` in this runnable example. `"안녕!"` occupies seven bytes but contains three characters, so a string’s byte length is not always its character count. section 1.3

How Learn Rust in a Month of Lunches covers it

How deep it goes
Explains signedness, integer widths, binary bits, character casts, and byte counts through examples rather than defining every primitive type.
Examples
Uses integer ranges, Unicode character literals, compiler feedback on a cast, and string byte-versus-character counts.
What it assumes
Defines signed integers, binary digits, and bits from basics while using small Rust `fn main` examples.
What you can do after
Evaluate/understand it by choosing Rust integer types and distinguishing a character count from a string’s byte length.
How it connects
Connects primitive types to type inference, binary numbers, character conversion, and string byte lengths.

Also explained in

Compare how 6 books explain primitive data type →

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.