Type annotations and inference
Quick answer
Every Rust value has a type. Type inference means the compiler can often determine it without an explicit annotation. Write let count: u32 = 12; when a specific type is important for your meaning or an API.
Annotations and literals
A colon after the binding name introduces the type. A suffix on a numeric literal can also specify a type. i32 holds signed 32-bit integers; u32 holds unsigned 32-bit integers. f64 represents a 64-bit floating-point number. Types constrain operations and values, not just how a number is printed.
fn main() { let seats: u32 = 24; let change: i32 = -3; println!("{seats}, {change}");}Expected stdout (not executed):
24, -3The signed type permits a negative value.
fn main() { let width = 4.5_f64; let area = width * 2.0; println!("{area}");}Expected stdout (not executed):
9The suffix establishes the type of width, and the multiplication constrains the other operand.
Common mistake
Do not assume Rust converts arbitrary numeric types automatically. Adding an explicitly typed i32 to an explicitly typed u32 requires a deliberate conversion.
Try it yourself
Declare a signed temperature, an unsigned count and a floating-point length. Print the three values.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Type annotations and inference — practice quiz
Choose one answer per question, then review the explanation.
Which declaration explicitly gives a signed integer type to a negative measurement?
What type does `let distance = 2.5_f64;` establish?
Why is `let qty: u32 = 3.5;` rejected?