我经常需要处理几何数据,在C++中,我通常会这样做:
struct Vertex { vec2;}
vector<Vertex> triangle = {{-1, 0}, {0,1}, {1, 0}};
这相当方便,特别是当您开始拥有更多嵌套类型时,比如向Vertex添加更多字段。
在 Rust 中,初始化程序需要明确指定,因此我会得到像这样的东西:
let triangle : [Vertex; 3] = [
Vertex{position : Vec2::new(-0.5, 0.0), color : Vec3::new(1.0, 0.0, 0.0)},
Vertex{position : Vec2::new(0.0, 0.5), color : Vec3::new(0.0, 1.0, 0.0)},
Vertex{position : Vec2::new(0.5, 0.0), color : Vec3::new(0.0, 0.0, 1.0)},
];
这有点过于繁琐,反复指定相同的字段变得乏味,即使在不太糟糕的情况下,当您拥有位置、法线和UV字段时,它也会变成一团糟。
是否有一种更紧凑的方式初始化列表?