Rust tips is a user on octodon.social. You can follow them or interact with them if you have an account anywhere in the fediverse. If you don't, you can sign up here.
Rust tips @rust

.collect() cannot infer type?

let data = vec![1,2,3].iter().map(|x|x*x).collect();
^^^^ cannot infer type for `_`

The expression is ambiguous, because .collect() can create not just Vec, but HashMap, String, and others as well: doc.rust-lang.org/std/iter/tra

Specify the type you want. It's not necessary to type it in full, e.g. `Vec<_>` is enough.

let data: Vec<_> = iter.collect();
let data = iter.collect::<Vec<_>>();

Try it: is.gd/yMg2gs

· Web · 0 · 2