Remove an element from the vector

Is there any other way of removing an element from the vector, other than finding the index of that element and using the remove method as like this :

let mut xs = vec![1,1,1,2,2,3];
let index = xs.iter().position(|x| *x == 1).unwrap();
xs.remove(index);

There are plenty of methods, depending on what exactly you want to do, e.g. retain(), dedup_by_key()

  1. Please format your code, so that it is more readable.
  2. For removing strictly one element (and not, for example, all elements equal to 1), your method might be the easiest.
1 Like

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