Types & Type Inference (Rust)
Rust is statically and strongly typed: every value has a type known at compile time. Type inference lets you usually skip writing types, while the compiler still checks them rigorously.
Rust
Rust's scalar types include integers (i32, u64, usize…), floats (f32, f64), bool, and char, plus compound types like tuples and arrays. Types are checked at compile time, so type mismatches are caught before the program runs and there are no implicit narrowing conversions.
Despite being statically typed, Rust feels lightweight because of powerful local type inference: let x = 5 infers i32, and the compiler propagates types through expressions. You add annotations where inference is ambiguous, such as let n: u64 = ... or collect::<Vec<_>>().
Conversions are explicit: use as for primitive casts (n as f64) and From/Into traits for richer conversions. This strictness, combined with inference, is what gives Rust both safety and ergonomics.
Syntax
let x = 5; // inferred i32
let y: f64 = 2.0; // annotated
let z = x as f64; // explicit castExamples
Rust
fn main() {
let count = 3; // i32 inferred
let ratio = 1.5_f64;
let total = count as f64 * ratio;
println!("{}", total);
}Output:
4.5