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

It's not possible to destructure and move from boxes without introducing an additional binding #22205

Closed
renato-zannon opened this issue Feb 12, 2015 · 3 comments

Comments

@renato-zannon
Copy link
Contributor

This program compiles:

struct NonCopy;

fn main() {
    let x = Box::new((NonCopy, NonCopy));

    let pair = *x;
    let (_a, _b) = pair;
}

But this one does not:

struct NonCopy;

fn main() {
    let x = Box::new((NonCopy, NonCopy));
    let (_a, _b) = *x;
}

It fails with:

destr.rs:6:14: 6:16 error: use of moved value: `x`
destr.rs:6     let (_a, _b) = *x;
                        ^~
destr.rs:6:10: 6:12 note: `x` moved here (through moving `x.0`) because it has type `NonCopy`, which is moved by default
destr.rs:6     let (_a, _b) = *x;
                    ^~
destr.rs:6:10: 6:12 help: use `ref` to override
destr.rs:6     let (_a, _b) = *x;
                    ^~
error: aborting due to previous error
@florence
Copy link

Another oddity, that seems related:

let x = Box::new(("","".to_string()));
let (a,b) = *x;

works but:

let x = Box::new(("".to_string(),""));
let (a,b) = *x;

fails with the same "moved value" error.

@goffrie
Copy link
Contributor

goffrie commented Feb 13, 2015

I believe this is the same as #16223.

@huonw
Copy link
Member

huonw commented Feb 13, 2015

Looks like it; thanks @goffrie (and @renato-zannon and @florence).

@florence I believe that one is because it does it in field order. "" is a &'static strwhich isCopy, so theabinding in the first one doesn't move out ofx, and hence doesn't invalidxfor theb` binding.

@huonw huonw closed this as completed Feb 13, 2015
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

4 participants