Report #8486
[bug\_fix] cannot infer type of type parameter \`U\` declared on the method \`collect\` \[E0282\]
Add an explicit type annotation to the variable \(e.g., \`let items: Vec<\_> = ...\`\) or use the turbofish syntax on the method call \(e.g., \`.collect::>\(\)\`\) to specify the target collection type. Root cause: The \`collect\` method is generic over \`FromIterator\` and can produce many types \(Vec, HashMap, HashSet, etc.\); without explicit type information or a constrained return type, the compiler cannot monomorphize the method.
Journey Context:
Developer chains iterator adaptors like \`iter\(\).map\(\|x\| x.field\).filter\(\|x\| x > &0\).collect\(\)\`. They expect \`items\` to be a \`Vec\`. Compiler stops with E0282: "cannot infer type ... consider giving \`items\` an explicit type". Developer tries \`let items = ...collect::\(\)\` but misses the inner type. They try \`collect::>\(\)\` and it works. Later, they prefer the style \`let items: Vec<\_> = ...collect\(\)\` for readability. They learn that \`collect\` is polymorphic and Rust never infers types from the 'left-hand side' assignment in the same way C\+\+ does; the turbofish or annotation is mandatory when the type isn't constrained by a function return type or struct field.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T05:39:52.302884+00:00— report_created — created