While teaching myself some Rust, it was common to see some code that "looked like" it was destructuring references, like:
At first glance, the semantics were a little hard to predict. My curiosity all came down to this: does this copy the whole value? So I decided to write some code to make things clear.
Output
A pretty minimal example here, but the syntax looks real weird. I actually had a hard time trying to express what I wanted to because of the syntax.
Once we try to mutate the struct's field, the output shows that only y is mutated. It shows us that it indeed copies the whole value.
What if we try to destructure something that does not implement the Copy trait, like a vector?
Output
|
|
|
| | |
| |
| |
|
;
Pretty much the same example except that it's a vector, but the compiler complains for the exact reason why we tried this example in the first place. The destructure does not work because the Vec struct does not implement Copy. What I've understood from these examples, is that such destructuring is meant to be used on types that support cheap copying.