[SOLVED]Importing values to vector


#1

Hello all,

I am coming from scripting languages like Python and Bash, and haven’t quite grasped idiomatic rust, not having experimented with systems programming languages before.

To jump right to the point, I am attempting to insert values into a vector from a different vector, maintaining their same places in the vector. The most obvious way seemed to me to be:

for i in 0..self.number {
    swapped_vector[i] = a2[i];
}

I am currently getting:

src/main.rs:73:13: 73:30 error: the trait `core::ops::Index<i32>` is not implemented for the type `collections::vec::Vec<i32>` [E0277]
src/main.rs:73             swapped_vector[i] = a2[i];
                           ^~~~~~~~~~~~~~~~~
src/main.rs:73:13: 73:30 help: run `rustc --explain E0277` to see a detailed explanation
src/main.rs:73:13: 73:30 note: the type `collections::vec::Vec<i32>` cannot be indexed by `i32`
src/main.rs:73:13: 73:30 error: the trait `core::ops::IndexMut<i32>` is not implemented for the type `collections::vec::Vec<i32>` [E0277]
src/main.rs:73             swapped_vector[i] = a2[i];
                           ^~~~~~~~~~~~~~~~~
src/main.rs:73:13: 73:30 help: run `rustc --explain E0277` to see a detailed explanation
src/main.rs:73:13: 73:30 note: the type `collections::vec::Vec<i32>` cannot be mutably indexed by `i32`
src/main.rs:73:33: 73:38 error: the trait `core::ops::Index<i32>` is not implemented for the type `collections::vec::Vec<i32>` [E0277]
src/main.rs:73             swapped_vector[i] = a2[i];
                                               ^~~~~
src/main.rs:73:33: 73:38 help: run `rustc --explain E0277` to see a detailed explanation
src/main.rs:73:33: 73:38 note: the type `collections::vec::Vec<i32>` cannot be indexed by `i32`

Would anyone be able to point me in the right direction?

P.S. I can share the whole file if this isn’t enough info.


#2

See http://doc.rust-lang.org/std/vec/struct.Vec.html
Vec has impl Index<usize>.
You can either make self.number as usize or cast i to usize.

for i in 0..self.number {
    let i = i as usize;
    swapped_vector[i] = a2[i];
}

#3

Casting i to usize worked like a charm. Thanks!