January 2019
Beginner to intermediate
554 pages
13h 31m
English
Type inference is useful in statically typed languages as it makes the code easier to write, maintain, and refactor. Rust's type system can figure out types for fields, methods, local variables, and most generic type arguments when you don't specify them. Under the hood, a component of the compiler called the type checker uses the Hindley Milner type inference algorithm to decide what the types of local variables should be. It is a set of rules about establishing types of expressions based on their usage. As such, it can infer types based on the environment and the way a type is used. One such example is the following:
let mut v = vec![];v.push(2); // can figure type of `v` now to be of Vec<i32>
With only the first line initializing ...
Read now
Unlock full access