Is it possible to specify destination types in destructuring assignment?

Viewed 54

One may destructure tuple like this:

let (x,y,z) = xyz;

But is it possible to specify types for x,y, and z? For example: xyz may be composed of f32-s but I'd like to get its components as f64-s. Unfortunately this code doesn't compile:

let (x:f64, y:f64, z:f64) = xyz;
1 Answers

Absolutely, you can annotate the type of the whole tuple:

let (x, y, z): (i32, &str, f32) = (10, "foo", 3.141516);

Playground

You can also partially specify them:

let (x, y, z): (i32, _, f32) = (10, "foo", 3.141516);

Note that this doesn't cast from one type to the others. In rustlang it needs to be done explicetly.

Related