Why is the type not inferred?

To expand briefly on what @CAD97 said, my understanding is that type checking is always done "in order," leading to errors like this.

During type checking, type inference is performed, which carries information backwards (allowing the type checker to know what the most general possible type of a variable is given how far it has gone through the function body). This often gives the illusion that the compiler can work backwards from the return type, and it works fine for things like trait bounds; but the illusion easily breaks in the face of features like type-directed method resolution.

.collect::<Vec<_>>() should be enough to make the snippet compile. You only need to specify the type far enough for the method to be found, and can leave placeholders for the rest.

1 Like