Lesson 3 - Data Types
Every value in Rust has a type.
A type tells Rust what kind of information a value represents.
-
Integers are whole numbers.
For example: let age: i32 = 16;
Rust has several integer types, each representing a different number of bits.
i8 i16 i32 i64 i128
The number represents how many bits are used.
There are also unsigned versions:
u8 u16 u32 u64 u128
Unsigned numbers cannot represent negative values.
-
Floating-point numbers contain decimals.
For example: let temperature: f64 = 72.5;
Rust commonly uses f32 and f64
f64 provides more precision and is commonly used when you need decimal calculations.
-
A Boolean has only two possible values, true or false.
Booleans are especially useful with if statements.
-
A char represents one Unicode character.
For example: let grade: char = 'A';
Notice that 'A' uses single quotes, while a string "A" uses double quotes.
Rust can often figure out a variable's type automatically.
let age = 16;
Rust knows that age is an integer.
You can also explicitly specify the type:
let age: i32 = 16;