Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why does index [] try to move value, but calling index directly does not

Example:

use std::ops::Index;

fn test(v: &Vec<Vec<i32>>) {  
    let t = v.index(0); //ok
    let t = v[0]; //error, Vec<i32> does not implement Copy trait
}

Playground

Why does that happen? As specified in the documentation:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

fn index(&self, index: I) -> &<Vec<T, A> as Index<I>>::Output

Performs the indexing (container[index]) operation

So it should be the same.

>Solution :

There is a hidden dereference when you use square brackets. The Index trait documentation says:

container[index] is actually syntactic sugar for *container.index(index)

If you add * to the first line then you get the same error message:

error[E0507]: cannot move out of a shared reference
 --> src/lib.rs:4:13
  |
4 |     let t = *v.index(0);
  |             ^^^^^^^^^^^ move occurs because value has type `Vec<i32>`, which does not implement the `Copy` trait
  |
help: consider removing the dereference here
  |
4 -     let t = *v.index(0);
4 +     let t = v.index(0);
  |

Playground

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading