Answer#
Rust infers most types, but it will not guess when several are possible. parse() can return any type that implements FromStr, and collect() can build a Vec, a HashSet, a String and many others. If nothing later in the function pins the type down, the compiler stops and asks. Depending on the code, the error code is E0282 or E0284; the fix is the same.
fn main() {
let numbers = "1 2 3".split(' ').map(|s| s.parse().unwrap()).collect();
println!("{:?}", numbers);
}error[E0284]: type annotations needed
--> src/main.rs:2:48
|
2 | let numbers = "1 2 3".split(' ').map(|s| s.parse().unwrap()).collect();
| ^^^^^ cannot infer type of the type parameter `F` declared on the method `parse`
|
= note: cannot satisfy `<_ as FromStr>::Err == _`
help: consider specifying the generic argument
|
2 | let numbers = "1 2 3".split(' ').map(|s| s.parse::<F>().unwrap()).collect();
| +++++
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0284`.The answers below are the two ways to say the type.