Інференція
Механізм виведення типів досить розумний. Він робить більше, ніж просто дивиться на тип виразу значення під час ініціалізації. Він також дивиться на те, як змінна використовується пізніше, щоб вивести її тип. Ось просунутий приклад виведення типів:
fn main() {
// Because of the annotation, the compiler knows that `elem` has type u8.
let elem = 5u8;
// Create an empty vector (a growable array).
let mut vec = Vec::new();
// At this point the compiler doesn't know the exact type of `vec`, it
// just knows that it's a vector of something (`Vec<_>`).
// Insert `elem` in the vector.
vec.push(elem);
// Aha! Now the compiler knows that `vec` is a vector of `u8`s (`Vec<u8>`)
// TODO ^ Try commenting out the `vec.push(elem)` line
println!("{:?}", vec);
}
Жодної анотації типу для змінних не знадобилося, компілятор задоволений, і програміст також!