January 2018
Beginner to intermediate
454 pages
10h 8m
English
The Rust compiler can automatically detect the type of a variable in most cases. However, for people reading the code, it's not always obvious what a code returns. An example? Sure!
let x = "a 10 11 coucou 12 14".split(' ')
.filter_map(|e| e.parse::<u32>().ok())
.filter(|x| x % 2 == 0)
.map(|s| format!("{}", s))
.collect::<Vec<_>>()
.join("::");
After reading the code carefully, you'll guess that x is a String. However, you needed to read all those closures to get it and even then, are you really sure of the type?
In such cases, it's strongly recommended to just add the type annotation:
let x: String = "a 10 11 coucou 12 14".split(' ') .filter_map(|e| e.parse::<u32>().ok()) .filter(|x| x % 2 == 0) .map(|s| format!("{}", s)) ...Read now
Unlock full access