Skip to content

Incorrect "moving prohibited due to outstanding loan" error when destructuring vector with owned contents #4886

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

Closed
bstrie opened this issue Feb 11, 2013 · 3 comments

Comments

@bstrie
Copy link
Contributor

bstrie commented Feb 11, 2013

fn main() {
    let split = [~1, ~2];

    match split {
        [] => die!(),
        [_head, ..tail] => tail
    };
}

I'm not 100% sure if this should be legal, but the error message it gives is baffling:

$ rustc split2.rs
split2.rs:4:10: 4:15 error: moving out of immutable local variable prohibited due to outstanding loan
split2.rs:4     match split {
                      ^~~~~
split2.rs:4:10: 4:15 note: loan of immutable local variable granted here
split2.rs:4     match split {
                      ^~~~~
error: aborting due to previous error

Note that the error only occurs if you create a head binding. Replacing _head with _ causes this error to go away.

@nikomatsakis
Copy link
Contributor

Actually this is legit. The error message should be improved, but there's an issue on that already: #4715

The problem is that the head binding induces a move, but the ..tail is effectively a borrow, which means that this pattern is both moving out of vec[0] and then borrowing vec[1...]. Since the borrow checker doesn't consider indicates, this results in an error.

@nikomatsakis
Copy link
Contributor

The fix, by the way, is "[copy head, ..tail]"

@nikomatsakis
Copy link
Contributor

or "[ref head, ..tail]"

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