Rust Data Types
Every value in Rust has a type. The type tells the compiler how much memory the value needs and which operations are allowed on it. Rust is statically typed: all types are known at compile time, although you rarely have to write them because the compiler infers them.
Scalar types
A scalar type holds a single value. Rust has four of them:
| Type | Examples | Description |
|---|---|---|
| Integer | 7, -300, 1_000_000 |
Whole numbers |
| Floating point | 3.14, -0.5, 2.0 |
Numbers with a fractional part |
Boolean (bool) |
true, false |
Yes/no values |
Character (char) |
'a', 'Ж', '🦀' |
One Unicode character, in single quotes |
fn main() {
let year: i32 = 2015;
let pi: f64 = 3.14159;
let is_stable: bool = true;
let mascot: char = '🦀';
println!("Rust 1.0 was released in {}", year);
println!("Pi is roughly {}", pi);
println!("Stable: {}", is_stable);
println!("Mascot: {}", mascot);
}Rust 1.0 was released in 2015
Pi is roughly 3.14159
Stable: true
Mascot: 🦀
Integer types
Integers come in several sizes. Types starting with i are signed (can be negative); types starting with u are unsigned (zero or positive only).
| Bits | Signed | Unsigned | Unsigned range |
|---|---|---|---|
| 8 | i8 |
u8 |
0 to 255 |
| 16 | i16 |
u16 |
0 to 65 535 |
| 32 | i32 |
u32 |
0 to about 4.29 billion |
| 64 | i64 |
u64 |
0 to about 1.8 × 10¹⁹ |
| 128 | i128 |
u128 |
enormous |
| pointer size | isize |
usize |
used for indexes and lengths |
If you do not specify a type, integers default to i32. Every type knows its limits through the MIN and MAX constants:
fn main() {
println!("u8 range: {} to {}", u8::MIN, u8::MAX);
println!("i8 range: {} to {}", i8::MIN, i8::MAX);
println!("i32 max: {}", i32::MAX);
println!("u64 max: {}", u64::MAX);
}u8 range: 0 to 255
i8 range: -128 to 127
i32 max: 2147483647
u64 max: 18446744073709551615
Integer literals can be written in different bases and with a type suffix:
| Literal | Value |
|---|---|
98_222 |
decimal |
0xff |
hexadecimal (255) |
0o77 |
octal (63) |
0b1010 |
binary (10) |
b'A' |
byte, u8 only (65) |
42u8 |
the number 42 as a u8 |
Note: Going past a type's limit is called overflow. In debug builds Rust stops the program with a panic instead of silently wrapping around; in release builds it wraps. Choose a type large enough for your data.
Floating-point types
Rust has f32 (single precision) and f64 (double precision). The default is f64, which is both precise and fast on modern CPUs. Floating-point numbers are approximations, so tiny rounding differences can appear:
fn main() {
let a = 0.1 + 0.2;
println!("0.1 + 0.2 = {}", a);
println!("Rounded: {:.2}", a);
let half: f32 = 1.0 / 2.0;
println!("Half: {}", half);
}0.1 + 0.2 = 0.30000000000000004
Rounded: 0.30
Half: 0.5
Characters and strings
A char holds exactly one Unicode character and uses single quotes. Text with any number of characters uses double quotes and is a string — either a borrowed &str or an owned String. Strings get their own chapter later.
fn main() {
let letter = 'k';
let greeting = "hello";
let owned = String::from("world");
println!("{} {} {}", letter, greeting, owned);
println!("Is '{}' alphabetic? {}", letter, letter.is_alphabetic());
}k hello world
Is 'k' alphabetic? true
Converting between types
Rust never converts number types silently. To turn one numeric type into another, use as:
fn main() {
let items: i32 = 7;
let people: i32 = 2;
let share = items as f64 / people as f64;
println!("Each person gets {} items", share);
let big: i32 = 300;
let small = big as u8; // 300 does not fit in a u8
println!("300 as u8 = {}", small);
}Each person gets 3.5 items
300 as u8 = 44
Be careful: as truncates values that do not fit (300 − 256 = 44). Later lessons show safer conversions such as u8::try_from.
Compound types
Rust also has types that group several values: tuples like (1, "a", true) and arrays like [1, 2, 3]. They are covered in the Data Structures section.
Test yourself
What is the default type of let n = 10;?
- u8
- i32
- i64
- usize
Which type can store the value -5?
- u8
- u32
- i16
- usize
How do you write a char literal?
- "a"
- 'a'
a- char(a)