Is there a way to partially destroy the structure?

I have a structure:

struct ThreeDPoint {
    x: f32,
    y: f32,
    z: f32
}

and I want to extract two of the three properties after creating 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 generates the following error:

error[E0027]: pattern does 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 would look like this:

let { x: my_x, y: my_y } = point;
+4
source share
2 answers

..as a field in a symbol structor a tuple means "and the rest":

let ThreeDPoint { x: my_x, y: my_y, .. } = point;

Read more about this in the book of rust .

+9
source

You can partially destroy the structure as follows:

let point = ThreeDPoint { x: 0.3, y: 0.4, z: 0.5 };
let ThreeDPoint { my_x, my_y, .. } = point;
+3
source

Source: https://habr.com/ru/post/1684062/


All Articles