No need to concern yourself about that, as long your algorithms are fast, this is Rust. You can easily have an iterator over indexes that maps over values (for each element in indexes, it retrieves a value).
fn main() {
let indexes = [1, 3, 4];
let values = ["data1", "data2", "data3", "data4", "data5"];
for value in indexes.iter().map(|&index| values[index]) {
println!("{}", value);
}
}
It's somewhat trickier if you need to take ownership of non-Copy values. Assumming indexes are sorted with no duplicates, you can do this.
fn retain_indexes<T, I>(values: &mut Vec<T>, indexes: I)
where I: IntoIterator<Item = usize>
{
// Precondition: Sorted indexes, no duplicates
let len = values.len();
let mut deleted = 0;
let mut indexes = indexes.into_iter().peekable();
for i in 0..len {
if indexes.peek() == Some(&i) {
indexes.next();
values.swap(i - deleted, i);
} else {
deleted += 1;
}
}
values.truncate(len - deleted);
}
fn main() {
let indexes = [1, 3, 4];
let mut values = vec!["data1", "data2", "data3", "data4", "data5"];
retain_indexes(&mut values, indexes.iter().cloned());
println!("{:?}", values);
}