I would recommend using Vec::truncate :
fn main() { let mut nums = vec![1, 2, 3, 4, 5]; let n = 2; let final_length = nums.len().saturating_sub(n); nums.truncate(final_length); println!("{:?}", nums); }
In addition, I
- used by
saturating_sub to handle the case when there are no elements in the <> t23> vector - used by
vec![] to easily build a vector of numbers - printed the whole vector in one go
Usually, when you are "something", you want to have these values. If you need values ββin another vector, you can use Vec::split_off :
let tail = nums.split_off(final_length);
If you want to access elements but donβt want to create a whole new vector, you can use Vec::drain :
for i in nums.drain(final_length..) { println!("{}", i) }
Shepmaster
source share