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

Can't directly destructure boxed values #33550

Closed
jdm opened this issue May 10, 2016 · 1 comment
Closed

Can't directly destructure boxed values #33550

jdm opened this issue May 10, 2016 · 1 comment

Comments

@jdm
Copy link
Contributor

jdm commented May 10, 2016

This compiles:

struct Foo {
  s: String,
  s2: String,
}

impl Foo {
    fn bar(self: Box<Self>) {
        let f = *self;
        let _a = f.s;
        let _b = f.s2;
    }
}

fn main() {
    let f = Foo { s: "hi".to_owned(), s2: "bye".to_owned() };
    Box::new(f).bar();
}

This does not:

struct Foo {
  s: String,
  s2: String,
}

impl Foo {
    fn bar(self: Box<Self>) {
        let Foo { s, s2 } = *self;
    }
}

fn main() {
    let f = Foo { s: "hi".to_owned(), s2: "bye".to_owned() };
    Box::new(f).bar();
}

Due to this error:

<anon>:8:22: 8:24 error: use of moved value: `self` [E0382]
<anon>:8         let Foo { s, s2 } = *self;
                              ^~
<anon>:8:22: 8:24 help: see the detailed explanation for E0382
<anon>:8:19: 8:20 note: `self` moved here (through moving `self.s`) because it has type `collections::string::String`, which is moved by default
<anon>:8         let Foo { s, s2 } = *self;
                           ^
<anon>:8:19: 8:20 help: if you would like to borrow the value instead, use a `ref` binding as shown:
<anon>:          let Foo { ref s, s2 } = *self;
error: aborting due to previous error
playpen: application terminated with error code 101

I don't understand why one form is legal and the other is not, and the error message does not make it clear to me.

@eefriedman
Copy link
Contributor

Duplicate of #30564, I think. (It's not legal because of rust-lang/rfcs#130.)

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

3 participants