Tuples types position & ergonomics

Why am I able to do this

let (x, y): (u16, u16) = (1, 2); --> WORKS

but not this

let (x: u16, y: u16) = (1, 2); --> DOES NOT WORK

It seems that the language server in VScode at least suggests the the types for x & y similar to the example #2 which does not work if I actually put in the types.

I can't give you a better answer than that the second snippet is not how let bindings and patterns work in Rust. Tuple patterns don't support type annotations inside them. No patterns do in fact. From the reference:

A let statement introduces a new set of variables, given by a pattern. The pattern is followed optionally by a type annotation and then either ends, or is followed by an initializer expression plus an optional else block.

Related, I guess:

Moreover the ascription describes/applies to the expression on the right (or the parameter of a function or closure), not the types of the bindings in the pattern per se.

Here's a contrived example. To understand it you need to know that ascribed let expressions can autoderef (described inline), and also how patterns work.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.