Expected `&mut Vec<usize>`, found `&mut Vec<Range<{integer}>>`

Hi !

I have a function with this signature :
fn get_alea(available: &mut Vec<usize>, avoid: &mut Vec<usize>) -> Option<usize>

I try to call it like this:

let mut i = vec![1..9];
let mut a = Vec::new();
get_alea(&mut i, &mut a);

But I don't find how to specify that I want to init the range with usize type in place of integer...
expected &mut Vec<usize>, found &mut Vec<Range<{integer}>>

I looked in documentation how to precise the type when creating range (Range in std::ops - Rust) but I wonder if there is another way to init a range of usize...
Can you help me please ?

You create a vector with a single element 1..9 here, not a vector with eight elements 1, 2, 3, 4, 5, 6, 7, 8. You can collect the range into such a vector though:

fn get_alea(available: &mut Vec<usize>, avoid: &mut Vec<usize>) {}

fn main() {
    let mut i = (1..9).collect();
    let mut a = Vec::new();
    get_alea(&mut i, &mut a);
}

Playground.

P.S.: Please read the post on code formatting. Correctly formatted code makes answering a lot easier.

5 Likes

Thank you very much. I now understand my mistake.
I replace with this, it works !

let mut i : Vec<usize> = (1..9).collect();
let mut a : Vec<usize> = Vec::new();
1 Like