Skip to content

Iterating through a vector with a reference corrupts the contents of the vector #16532

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
paigeruten opened this issue Aug 16, 2014 · 1 comment

Comments

@paigeruten
Copy link

$ rustc -v
rustc 0.12.0-pre-nightly (e189122e9 2014-08-13 00:41:22 +0000)
$ cat bug.rs
fn main() {
    let vector: Vec<Vec<u8>> = vec![vec!(1, 2, 3), vec!(4, 5, 6)];

    println!("{}", vector);

    for &x in vector.iter() {
    }

    println!("{}", vector);
}

$ rustc bug.rs && ./bug
[[1, 2, 3], [4, 5, 6]]
[[], []]
@huonw
Copy link
Member

huonw commented Aug 16, 2014

I believe this is a dupe of #16205, which was fixed very recently by #16458 (just after the nightly compiler you are using); the compiler now correctly emits an error message, http://is.gd/dtQa8z

<anon>:6:9: 6:11 error: cannot move out of dereference of `&`-pointer
<anon>:6     for &x in vector.iter() {
                 ^~
<anon>:6:10: 6:11 note: attempting to move value to here (to prevent the move, use `ref x` or `ref mut x` to capture value by reference)
<anon>:6     for &x in vector.iter() {
                  ^

Thanks for filing!

@huonw huonw closed this as completed Aug 16, 2014
bors added a commit to rust-lang-ci/rust that referenced this issue Feb 18, 2024
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