0
0
Rustprogramming~10 mins

Type inference in Rust - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Type inference
Start: Declare variable without type
Compiler guesses type from value
Use variable in code
Compiler checks if usage matches guessed type
If matches, continue
If mismatch, error
End
Rust compiler guesses variable types from assigned values and checks usage to ensure correctness.
Execution Sample
Rust
fn main() {
    let x = 5;
    let y = 3.14;
    let z = "hello";
    println!("{} {} {}", x, y, z);
}
This code declares variables without explicit types; Rust infers their types from the assigned values.
Execution Table
StepCode LineActionInferred TypeNotes
1let x = 5;Declare x with value 5i32Integer literal defaults to i32
2let y = 3.14;Declare y with value 3.14f64Floating point literal defaults to f64
3let z = "hello";Declare z with value "hello"&strString slice type inferred
4println!("{} {} {}", x, y, z);Use variables in printx:i32, y:f64, z:&strTypes used match inferred types
5End of mainProgram runs successfully-No type errors found
💡 All variables have inferred types matching their usage, so compilation succeeds.
Variable Tracker
VariableStartAfter DeclarationFinal
xundefined5 (i32)5 (i32)
yundefined3.14 (f64)3.14 (f64)
zundefined"hello" (&str)"hello" (&str)
Key Moments - 3 Insights
Why doesn't Rust need me to write the type for 'x'?
Rust looks at the value '5' assigned to 'x' and guesses it is an integer (i32). This is shown in execution_table row 1.
What happens if I use 'x' as a floating number later?
Rust will give a type error because 'x' was inferred as i32, but using it as a float conflicts. This would fail at step 4 in the execution_table.
How does Rust know 'z' is a string slice?
Because 'z' is assigned a string literal "hello", Rust infers the type &str as shown in execution_table row 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2. What type does Rust infer for variable 'y'?
Af64
Bi32
C&str
Dbool
💡 Hint
Check the 'Inferred Type' column in execution_table row 2.
At which step does Rust check if variable usage matches the inferred type?
AStep 1
BStep 4
CStep 3
DStep 5
💡 Hint
Look at the 'Action' column where variables are used in println! macro.
If you changed 'let x = 5;' to 'let x = 5.0;', how would the inferred type change?
AFrom i32 to &str
BFrom f64 to i32
CFrom i32 to f64
DNo change, stays i32
💡 Hint
Consider how Rust infers types from numeric literals as shown in variable_tracker.
Concept Snapshot
Type inference in Rust:
- Declare variables without explicit types.
- Rust guesses type from assigned value.
- Integer literals default to i32.
- Floating literals default to f64.
- Compiler checks usage matches inferred type.
- Mismatches cause compile errors.
Full Transcript
This example shows how Rust infers types automatically. When you write 'let x = 5;', Rust sees the number 5 and decides 'x' is an integer (i32). For 'let y = 3.14;', Rust infers a floating-point number (f64). For string literals like 'let z = "hello";', Rust infers a string slice type (&str). The compiler then checks if you use these variables in ways that fit their types. If you try to use 'x' as a float later, Rust will give an error because it expects an integer. This process helps you write less code while keeping safety. The execution table shows each step of this inference and checking process.