我可以这样拆开一个经典的元组:
let pair = (1, true);
let (one, two) = pair;
如果我有一个元组结构体,例如struct Matrix(f32, f32, f32, f32)
,当我尝试解包它时,会出现"意外类型"的错误:
如果我有一个类似于struct Matrix(f32, f32, f32, f32)
的元组结构体,并尝试对其进行解构,我会收到一个关于“意外类型”的错误:
struct Matrix(f32, f32, f32, f32);
let mat = Matrix(1.1, 1.2, 2.1, 2.2);
let (one, two, three, four) = mat;
导致此错误的结果:
error[E0308]: mismatched types
--> src/main.rs:47:9
|
47 | let (one, two, three, four) = mat;
|
= note: expected type `Matrix`
found type `(_, _, _, _)`
我该如何解构一个元组结构体?我需要将其显式转换为元组类型吗?还是需要硬编码实现?