Skip to content
Theory · Variables and Types · lesson 6 of 12

Numbers, bool and char

In the first lesson of this theme, the type was the shape of a box: text does not go into a box for whole numbers. Now let's see which shapes there are.

Rust's simple types hold one value each and fall into four families: whole numbers, fractional numbers, logical values and characters. Each family has its own boxes, and Rust makes sure a value lies in a box of its own shape.

Rust
fn main() {
    let count = 42;
    let small: u8 = 255;
    let price = 9.99;
    let ready = true;
    let letter = 'R';
    println!("{count} {small} {price} {ready} {letter}"); // 42 255 9.99 true R
}

Let's go through the variables one by one:

  1. count is a whole number. No type is written, and the compiler picks i32.
  2. small is a whole number too, but of type u8: it holds numbers from 0 to 255.
  3. price is a fractional number. For numbers with a point, the compiler picks the type f64.
  4. ready is a logical value of type bool.
  5. letter is one character of type char.

Whole numbers

The name of an integer type reads in parts. The letter i means the number can be negative, and u means it can only be non-negative. The digits are the size of the number in bits. A bit is a memory cell for a zero or a one, and the more bits, the more different numbers fit. Four types come up most often:

  • i32 holds about −2.1 to 2.1 billion, and Rust picks it by default;
  • i64 is for large numbers, such as a count of milliseconds;
  • u8 holds 0 to 255, which is 8 bits, or one byte;
  • usize is for the length of something and the position of an element.

You can attach the type right to the number: 255u8 or 300_i32. The compiler skips underscores inside a number, so a million is handy to write as 1_000_000.

Here the box analogy falls short. A small thing fits into a big real box. But Rust does not put a u8 value into an i32 variable on its own, even though the number 255 would fit there too: the types must match.

Fractional numbers and division

Fractional numbers are held by the types f64 and f32. By default Rust takes f64: it is more precise. A number with a point, even 7.0, is always fractional.

How division works depends on the type:

Rust
println!("{}", 7 / 2); // 3
println!("{}", -7 / 2); // -3
println!("{}", 7.0 / 2.0); // 3.5
  1. 7 / 2 divides a whole number by a whole number, and the result is whole too. The fraction is dropped, and the result is 3. This is not rounding: 3.5 does not become 4.
  2. -7 / 2 also drops the fraction: -3.5 turns into -3.
  3. 7.0 / 2.0 divides fractional numbers and keeps the fraction: 3.5.

Interviewers often ask which type Rust picks for let x = 42; and for let y = 4.2;. The answer: i32 and f64.

Printing with {:?}

The {} placeholder prints a value the way a person finds it convenient to read, so it shows the fractional 3.0 as 3. The second placeholder, {:?}, shows a value the way it is written in code. This is debug output: it shows what really lies in a variable.

Rust
let whole = 3.0;
let letter = 'R';
let name = "Anna";
println!("{whole} {letter} {name}"); // 3 R Anna
println!("{whole:?} {letter:?} {name:?}"); // 3.0 'R' "Anna"
  1. {whole} prints 3, and {whole:?} prints 3.0: you can see that the number is fractional.
  2. {letter:?} adds single quotes, and {name:?} adds double quotes, as in the code.

Like {}, the debug placeholder works both with an argument after the string, {:?}, and with a name inside the braces, {whole:?}.

bool and char

bool

The type bool holds a "yes" or "no" answer: true or false. It is not a number, and you cannot add true to 1.

char

The type char holds one character: a Latin or Russian letter, a digit or even an emoji. It is written in single quotes: 'R', 'Ж', '🦀'. Every char takes 4 bytes of memory. In double quotes, "R" is no longer a character but a string: text of any number of characters.

Common mistake

A whole number and a fractional number were added:

Rust
fn main() {
    let total = 2 + 0.5; // ✗
    println!("{total}");
}
TEXT
error[E0277]: cannot add a float to an integer

For Rust, 2 and 0.5 are values of different types, and only values of the same type can be added. The compiler does not turn a whole number into a fractional one on its own. The fix is to write both numbers as fractional: 2.0 + 0.5 gives 2.5.

Key points

  • Integer types: i32 by default, i64 for large numbers, u8 from 0 to 255, usize for lengths and positions.
  • A number with a point is an f64; integer division drops the fraction: 7 / 2 is 3.
  • {} prints 3.0 as 3, while {:?} shows the value as in the code: 3.0, 'R', "Anna".
  • bool is true or false, and char is one character in single quotes.