What Is Type Inference in Rust?

Type inference allows the Rust compiler to automatically determine variable types from context, eliminating the need for explicit type annotations.

Type inference in Rust is the compiler's ability to deduce the data type of a variable from its usage without requiring explicit type annotations. You enable this by omitting the : Type syntax when declaring a variable with let, allowing the compiler to determine the type based on the assigned value or function context.

let guess = String::new(); // Compiler infers 'guess' is a String
let x = 5; // Compiler infers 'x' is an i32

This feature reduces boilerplate while maintaining strict type safety, as the compiler still checks that types match where they are used.