Generating a Vec<i32> of 0 .. n

    let n: usize = 1000;
    let mut ans: Vec<i32> = vec![];
    for i in 0..n {
        ans.push(i as i32)

Is there a more function way to write this code? (Yes, I do need it to be i32 output).

let ans: Vec<_> = (0..n as i32).collect();

I was trying into_vec and to_vec, but forgot about collect

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.