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

array indexing may not move #4

Open
lcnr opened this issue Sep 4, 2023 · 1 comment
Open

array indexing may not move #4

lcnr opened this issue Sep 4, 2023 · 1 comment

Comments

@lcnr
Copy link
Owner

lcnr commented Sep 4, 2023

fn fails_1(x: [String; 3], y: usize) {
    let q: String = x[y];
    //~^ ERROR cannot move out of type `[String; 3]`, a non-copy array
}

fn works_1(x: [String; 3], y: usize) {
    let _: String = x[y];
}

fn fails_2(x: [String; 3]) {
    let q: String = x[1];
    //~^ ERROR cannot move out of type `[String; 3]`, a non-copy array
}

fn works_2(x: [String; 3]) {
    let [_, q, _] = x;
    let [a, _, b] = x;
}
@lcnr
Copy link
Owner Author

lcnr commented Dec 18, 2024

however, we do consider there to be sub-places shared by all array elements, allowing the following to compile

fn main() {
    let mut x: [(u32, u32); 3] = Default::default();
    
    let y = &mut x[1].0;
    let z = &mut x[0].1;
    drop((y, z));
}

while this does not as all elements share a single place in borrowck

fn main() {
    let mut x: [(u32, u32); 3] = Default::default();
    
    let y = &mut x[1].0;
    let z = &mut x[0].0;
    drop((y, z));
}

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

1 participant