Explicit types in iterations/for loops

It would be nice if one could explicitly annotate the expected type of a variable in an iteration for example, consider this:

The matrix type is aliased to Vec3 in this codebase, for readability. But rust analyzer defaults to the original generic type instantiation, which is very arcane compared to just Vec3. In this case it would be nice to allow the programmer to take over and explicitly annotate the type, so that other people reading this snippet have an easier time understanding what is happening.

1 Like

RFC 2522, but it's postponed.

As a workaround, you can do

for normal in &mexhes[0].normals {
    let normal: &Vec3 = normal;

if you ever want to emphasize it.

(I do think for patterns should be annotatable.)


Is there any reason they currently aren't, other than "they're mostly useless"?

Well, the pattern grammar doesn't include types, so anywhere with a type annotation needs to be done separately today, and I guess it just wasn't here.

And I don't think they're that useless -- something like for x: u8 in 0..100 reads better to me than today's for x in 0_u8..100.

What I think I'd like to see is a smaller version of 2522 that just adds type annotations to bindings in patterns, rather than everywhere, as I think that's far less controversial. See this zulip thread about it.


This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.