rust - Is there a way to destructure a struct partially? -
i have struct:
struct threedpoint { x: f32, y: f32, z: f32 }
and want extract two of 3 properties after instantiating it:
let point: threedpoint = threedpoint { x: 0.3, y: 0.4, z: 0.5 }; let threedpoint { x: my_x, y: my_y } = point;
the compiler throws following error:
error[e0027]: pattern not mention field `z` --> src/structures.rs:44:9 | 44 | let threedpoint { x: my_x, y: my_y } = point; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ missing field `z`
in javascript (es6), equivalent destructuring this:
let { x: my_x, y: my_y } = point;
..
field in struct
or tuple pattern means "and rest":
let threedpoint { x: my_x, y: my_y, .. } = point;
there's more in the rust book.
Comments
Post a Comment