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.
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:
countis a whole number. No type is written, and the compiler picksi32.smallis a whole number too, but of typeu8: it holds numbers from 0 to 255.priceis a fractional number. For numbers with a point, the compiler picks the typef64.readyis a logical value of typebool.letteris one character of typechar.
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:
i32holds about −2.1 to 2.1 billion, and Rust picks it by default;i64is for large numbers, such as a count of milliseconds;u8holds 0 to 255, which is 8 bits, or one byte;usizeis 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:
println!("{}", 7 / 2); // 3
println!("{}", -7 / 2); // -3
println!("{}", 7.0 / 2.0); // 3.57 / 2divides a whole number by a whole number, and the result is whole too. The fraction is dropped, and the result is3. This is not rounding:3.5does not become4.-7 / 2also drops the fraction:-3.5turns into-3.7.0 / 2.0divides 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.
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"{whole}prints3, and{whole:?}prints3.0: you can see that the number is fractional.{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:
fn main() {
let total = 2 + 0.5; // ✗
println!("{total}");
}error[E0277]: cannot add a float to an integerFor 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:
i32by default,i64for large numbers,u8from 0 to 255,usizefor lengths and positions. - A number with a point is an
f64; integer division drops the fraction:7 / 2is3. {}prints3.0as3, while{:?}shows the value as in the code:3.0,'R',"Anna".boolistrueorfalse, andcharis one character in single quotes.