let n: usize = 1000;
let mut ans: Vec<i32> = vec![];
for i in 0..n {
ans.push(i as i32)
}
ans
Is there a more function way to write this code? (Yes, I do need it to be i32 output).
let n: usize = 1000;
let mut ans: Vec<i32> = vec![];
for i in 0..n {
ans.push(i as i32)
}
ans
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
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.