Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moving out of boxed structs by destructuring no longer works #28536

Closed
apoelstra opened this issue Sep 20, 2015 · 2 comments
Closed

Moving out of boxed structs by destructuring no longer works #28536

apoelstra opened this issue Sep 20, 2015 · 2 comments

Comments

@apoelstra
Copy link
Contributor

In 1.3.0,

struct PatriciaTree {
    data: Box<u8>,
    child_l: Box<u8>,
}

impl PatriciaTree {
    fn delete(&mut self) {
        let child = Box::new(PatriciaTree { data: Box::new(5), child_l: Box::new(5) });
        let PatriciaTree { data, child_l } = *child;
    }
}

fn main() {}

fails with

break3.rs:10:34: 10:41 error: use of moved value: `child` [E0382]
break3.rs:10         let PatriciaTree { data, child_l } = *child;
                                              ^~~~~~~
break3.rs:10:28: 10:32 note: `child` moved here (through moving `child.data`) because it has type `Box<u8>`, which is moved by default
break3.rs:10         let PatriciaTree { data, child_l } = *child;
                                        ^~~~
break3.rs:10:28: 10:32 help: if you would like to borrow the value instead, use a `ref` binding as shown:
break3.rs:           let PatriciaTree { ref data, child_l } = *child;
error: aborting due to previous error

This is a common pattern for moving all the fields at once out of a struct (since moving them one by one could introduce memory unsafety).

Edit: I posted the wrong error message in the first version of this bug; fixed now.

@apoelstra
Copy link
Contributor Author

Removing the box by adding the line let child = *child before destructuring is a workaround.

@apoelstra apoelstra changed the title Moving out of structs by destructuring no longer works Moving out of boxed structs by destructuring no longer works Sep 20, 2015
@alexcrichton
Copy link
Member

Duplicate of #16223, but thanks for the report!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants