How can I unpack a tuple struct like I would a classic tuple?

谁说胖子不能爱 提交于 2019-12-05 01:06:20

It's simple: just add the type name!

struct Matrix(f32, f32, f32, f32);

let mat = Matrix(1.1, 1.2, 2.1, 2.2);
let Matrix(one, two, three, four) = mat;
//  ^^^^^^

This works as expected.


It works exactly the same with normal structs. Here, you can bind either to the field name or a custom name (like height):

struct Point {
    x: f64,
    y: f64,
}

let p = Point { x: 0.0, y: 5.0 };
let Point { x, y: height } = p;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!