

Rust for C++ Programmers – Part 9 - valarauca1
http://featherweightmusings.blogspot.com/2014/07/rust-for-c-programmers-part-9.html?m=1

======
thristian
I appreciate this is "for C++ programmers" but as a Python programmer I'm
still interested.

I'm trying to figure out why destructuring something into multiple variables
causes problems for move semantics. It seems to me that if you have an object
with move semantics in a particular area of memory, and you tell the compiler
"I wish to use the name X to refer to _this_ field, and the name Y to refer to
_that_ field", that should be fine - as long as you don't store a reference to
those fields, or pass them to somebody else (the usual restrictions of
borrowed code). The compiler already knows that some names refer to
allocations on the stack or the heap, it seems like you should be able to add
new names for existing variables at compile-time (as opposed to runtime, using
pointers which would add all the horrible borrowing issues Rust tries to
avoid).

Is it really just that when you create a new name, the compiler has to create
a new allocation to back it, even if it would require making a redundant copy
of the interesting data?

~~~
kwantam
You can destructure without moving, as you suggest, by taking a reference.

Move:

    
    
        let x = (box 2u, box 4u);
        let (y, z) = match x {
            (first, second) => (first, second)
        };
        println!("x is {}", x);
        // this will not compile: x is moved into y and z
    

Reference:

    
    
        let x = (box 2u, box 4u);
        let (y, z) = match x {
            (ref first, ref second) => (first, second)
        };
        println!("x is {}", x);
        // this works fine

~~~
dbaupp
(The `match` is unnecessary in both examples: the patterns are irrefutable and
so work directly in `let`.)

------
aminbandali
As usual, I've updated the GitBook. Part 9 is now in there. Read:
[https://www.gitbook.io/book/aminb/rust-
for-c](https://www.gitbook.io/book/aminb/rust-for-c)

