Cannot return vector slice - ops :: Range <i32> not implemented

I am puzzled as to why the following Rust code is giving an error

fn getVecSlice(vec: &Vec<f64>, start: i32, len: i32) -> &[f64]


The error message I am getting is

the trait `core::ops::Index<core::ops::Range<i32>>` is not implemented for the type `collections::vec::Vec<f64>` [E0277]


What I am trying to do is simulate a 2 dimensional matrix using the std :: Vec class and return references to different rows of the matrix. What is the best way to achieve this?


source to share

1 answer

The error messages indicated that you cannot index into a vector with type values u32

. Indices Vec

must be of a type usize

, so you need to cast your indices to that type like this:

vec[start as usize .. (start + len) as usize]


or just change the type of the arguments start

and len

to usize




All Articles