Rust Data Types
A data type is a label that says what kind of value a value is. Rust checks types while compiling, before the program runs.
A value with its type named
Write the type after a colon to say exactly what kind of value a variable holds:
let age: u8 = 30;
println!("age = {}", age);The four scalar types
A scalar type holds one value: a whole number, a decimal number, a true-or-false value, or a single character:
let whole: i32 = -3;
let price: f64 = 19.99;
let ok: bool = true;
let initial: char = 'R';
println!("{} {} {} {}", whole, price, ok, initial);A suffix pins the type to the value
The type can ride on the value itself: 42u8 is the number 42, held as a u8:
let small = 42u8;
println!("{}", small);Tuples and arrays group values
Compound types group values together: a tuple mixes types in one group, an array repeats one type:
let pair = (7, "seven");
let scores = [1, 2, 3];
println!("{:?} {:?}", pair, scores);The unit type ()
A call with nothing to hand back still returns something: the single value of the unit type, written (). println! is one such call:
let got = println!("Hello!");
println!("the call gave back {:?}", got);Example
fn main() {
let age: u8 = 30;
let price: f64 = 19.99;
let ok: bool = true;
let initial: char = 'R';
let pair = (7, "seven");
println!("{} {} {} {} {:?}", age, price, ok, initial, pair);
}Key points
- Types come in two families.
- Scalar types hold one value, like a number, a bool, or a char.
- Compound types group values together, like tuples and arrays.
- Every type must be known while compiling: Rust is statically typed.
- A type note like let x: u32 is needed when Rust cannot guess.
- Turning text into a number is the classic case.
- When Rust guesses, whole numbers default to i32 and decimals default to f64
In this example, the first four variables name their types after a colon. age is a small whole number, price is a decimal, ok is true, and initial is the single character 'R'. Note the quotes: a char uses single quotes, but text uses double quotes. pair names no type. Its tuple type is guessed from the values it holds: a number and a word together. The println! prints each value, and {:?} shows the tuple in its detailed form.
Try it Yourself
Edit the code, then press Run.
Running is simulated · Rust Playground
The types in this lesson
| Type | What it holds | Size | Range or example |
|---|---|---|---|
| i32 | A whole number, with a sign | 4 bytes | -2,147,483,648 to 2,147,483,647 |
| u32 | A whole number, zero and up | 4 bytes | 0 to 4,294,967,295 |
| u8 | A small whole number, zero and up | 1 byte | 0 to 255 |
| f64 | A decimal number | 8 bytes | 19.99 |
| bool | True or false | 1 byte | true |
| char | One character | 4 bytes | 'R' |
| tuple | A group of values, types can differ | Depends on the values inside | (7, "seven") |
| array | A group of values, all one type | The count times the size of one value | [1, 2, 3] |
| () | The unit type, with one value | 0 bytes | () — what a function with no return gives back |
Exercise
What exactly is a Rust char?